//
//  XMLParser.m
//  Portable Traffic Signal Assistor
//
//  Created by Hao Xu on 4/1/12.
//  Copyright (c) 2012 TTU. All rights reserved.
//

#import "XMLParser.h"

@implementation XMLParser

static GDataXMLDocument *dataDoc=nil;
static GDataXMLDocument *dataTempDoc=nil;
static NSString *dataFileName=@"signal_assistor_app";
static NSString *dataXMLRoot=@"//TransIntelligence/SignalAssistor";

+ (void) loadDataXML
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                      NSUserDomainMask, YES);
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentsPath = [documentsDirectory
                               stringByAppendingPathComponent:[dataFileName stringByAppendingPathExtension:@"xml"]];
    NSLog(@"%@ \n", documentsPath);
    if(![[NSFileManager defaultManager] fileExistsAtPath:documentsPath])
    {
        
        [XMLParser createNewProject];
    }
    
    
    NSData *xmlData = [[[NSMutableData alloc] initWithContentsOfFile:documentsPath]autorelease];
    
    NSError *error;
    dataDoc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    
    
    NSLog(@"%@ \n", dataDoc.rootElement);
}

+ (void) loadTempXML
{
    NSString *filePath = [XMLParser dataTempFilePath];
    NSData *xmlData = [[[NSMutableData alloc] initWithContentsOfFile:filePath]autorelease];
    NSError *error;
    dataTempDoc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
}

+ (NSString *)dataTempFilePath;
{
    return [[NSBundle mainBundle] pathForResource:@"dataTempXML" ofType:@"xml"];
}

+ (void) saveDataXML
{
    
  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentsPath = [documentsDirectory stringByAppendingPathComponent:[dataFileName stringByAppendingPathExtension:@"xml"]];
    NSLog(@"Saving xml data to %@...", documentsPath);
//    NSString* formattedXML = [dataDoc.rootElement XMLStringWithOptions:NSXMLNodePrettyPrint];
    [dataDoc.XMLData writeToFile:documentsPath atomically:YES];
}
+ (void) createNewProject
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentsPath = [documentsDirectory
                               stringByAppendingPathComponent:[dataFileName stringByAppendingPathExtension:@"xml"]];
    NSLog(@"Saving xml data to %@...", documentsPath);
    if (!dataTempDoc) {
        [XMLParser loadTempXML];
    }
    [dataTempDoc.XMLData writeToFile:documentsPath atomically:YES];
//    [self saveDataXML];
    //    [XMLParser saveProjectEditTime];
    
}










+ (NSArray *)getTimerGroupList
{
    NSString *xPathString=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup"];
    NSLog(@"\n %@!!", xPathString);
    
    NSArray *timerGroupElementList;
    if (dataDoc!=nil) {
        timerGroupElementList=[dataDoc nodesForXPath:xPathString error:nil];
    }else {
        return nil;
    }
    NSMutableArray *timerGroupList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement *timerGroupElement in timerGroupElementList) {
        TimerGroup * timerGroup=[[[TimerGroup alloc] init] autorelease];
        NSArray *IDArray=[timerGroupElement elementsForName:@"TimerGroupID"];
        if (IDArray.count>0) {
            [timerGroup setTimerGroupID:[[IDArray objectAtIndex:0] stringValue]];
            NSMutableArray *signalList=[XMLParser getSignalListByTimerGroupID:timerGroup.timerGroupID];
            [timerGroup setSignalList:signalList];
        }
        NSArray * syncTimeArray=[timerGroupElement elementsForName:@"SyncTime"];
        if (syncTimeArray.count>0) {
            [timerGroup setSyncTime:[[syncTimeArray objectAtIndex:0] stringValue]];
        }
        [timerGroupList addObject:timerGroup];
        
    }
    
    
    
    return timerGroupList;
}
+ (TimerGroup *)getTimerGroup:(NSString *)timerGroupID
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
    NSString *xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
    
    NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]"];
    NSArray *timerGroupElementList;
    if (dataDoc!=nil) {
        timerGroupElementList=[dataDoc nodesForXPath:xPathString3 error:nil];
    }else {
        return nil;
    }
//    NSMutableArray *timerGroupList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement *timerGroupElement in timerGroupElementList) {
        TimerGroup * timerGroup=[[[TimerGroup alloc] init] autorelease];
        NSArray *IDArray=[timerGroupElement elementsForName:@"TimerGroupID"];
        if (IDArray.count>0) {
            [timerGroup setTimerGroupID:[[IDArray objectAtIndex:0] stringValue]];
            NSMutableArray *signalList=[XMLParser getSignalListByTimerGroupID:timerGroup.timerGroupID];
            [timerGroup setSignalList:signalList];
        }
        NSArray * syncTimeArray=[timerGroupElement elementsForName:@"SyncTime"];
        if (syncTimeArray.count>0) {
            [timerGroup setSyncTime:[[syncTimeArray objectAtIndex:0] stringValue]];
        }
        return timerGroup;
        
    }
return nil;
}
+ (NSMutableArray *)getSignalListByTimerGroupID:(NSString *) timerGroupID
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
    NSString *xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
    
    NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]/Signals/Signal"];
    
    NSArray *signalElementList=[dataDoc nodesForXPath:xPathString3 error:nil];
    
    NSMutableArray * signalList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement * signalElement in signalElementList) {
        Signal *signal=[[[Signal alloc] init] autorelease];
        NSArray *elementArray=[signalElement elementsForName:@"SignalID"];
        if (elementArray.count>0) {
            [signal setSignalID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"SignalName"];
        if (elementArray.count>0) {
            [signal setSignalName:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"CycleLength"];
        if (elementArray.count>0) {
            [signal setCycleLength:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"Offset"];
        if (elementArray.count>0) {
            [signal setOffset:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"Location"];
        if (elementArray.count>0) {
            [signal setLocation:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"Ring1"];
        if (elementArray.count>0) {
            SignalRing *ring1=[[[SignalRing alloc]init] autorelease];
            GDataXMLElement *ring1Element=[elementArray objectAtIndex:0];
            NSArray *ringSubElementArray=[ring1Element elementsForName:@"RingID"];
            if (ringSubElementArray.count>0) {
                [ring1 setRingID:[[ringSubElementArray objectAtIndex:0] stringValue]];
            }
            ringSubElementArray=[ring1Element elementsForName:@"Phases"];
            if (ringSubElementArray.count>0) {
                NSArray *phaseElementArray=[[ringSubElementArray objectAtIndex:0] elementsForName:@"Phase"];
                NSMutableArray *phaseList=[[[NSMutableArray alloc]init]autorelease];
                for (GDataXMLElement *phaseElement in phaseElementArray) {
                    SignalPhase *signalPhase=[[[SignalPhase alloc] init] autorelease];
                    NSArray *phaseSubElementArray=[phaseElement elementsForName:@"PhaseID"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseID:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"PhaseMovement"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseMovement:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"GreenTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setGreenTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"YellowTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setYellowTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"AllRedTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setAllRedTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    [phaseList addObject:signalPhase];
                }
                [ring1 setPhaseList:phaseList];
            }
            [signal setRing1:ring1];
            
        }
        elementArray=[signalElement elementsForName:@"Ring2"];
        if (elementArray.count>0) {
            SignalRing *ring2=[[[SignalRing alloc]init] autorelease];
            GDataXMLElement *ring2Element=[elementArray objectAtIndex:0];
            NSArray *ringSubElementArray=[ring2Element elementsForName:@"RingID"];
            if (ringSubElementArray.count>0) {
                [ring2 setRingID:[[ringSubElementArray objectAtIndex:0] stringValue]];
            }
            ringSubElementArray=[ring2Element elementsForName:@"Phases"];
            if (ringSubElementArray.count>0) {
                NSArray *phaseElementArray=[[ringSubElementArray objectAtIndex:0] elementsForName:@"Phase"];
                NSMutableArray *phaseList=[[[NSMutableArray alloc]init]autorelease];
                for (GDataXMLElement *phaseElement in phaseElementArray) {
                    SignalPhase *signalPhase=[[[SignalPhase alloc] init] autorelease];
                    NSArray *phaseSubElementArray=[phaseElement elementsForName:@"PhaseID"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseID:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"PhaseMovement"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseMovement:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"GreenTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setGreenTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"YellowTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setYellowTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"AllRedTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setAllRedTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    [phaseList addObject:signalPhase];
                }
                [ring2 setPhaseList:phaseList];
            }
            [signal setRing1:ring2];
            
        }
        [signalList addObject:signal];
        
        
    }
    return signalList;
}
+ (Signal *)getSignalByTimerGroupIDAndSignalID:(NSString *)timerGroupID signalID:(NSString *)signalID
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
    NSString *xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
    
    NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]/Signals/Signal[SignalID=\""];
    NSString *xPathString4=[xPathString3 stringByAppendingString:signalID];
    NSString *xPathString5=[xPathString4 stringByAppendingString:@"\"]"];
    NSArray *signalElementList=[dataDoc nodesForXPath:xPathString5 error:nil];
    NSLog(@"%@",xPathString5);
//    NSMutableArray * signalList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement * signalElement in signalElementList) {
        Signal *signal=[[[Signal alloc] init] autorelease];
        NSArray *elementArray=[signalElement elementsForName:@"SignalID"];
        if (elementArray.count>0) {
            [signal setSignalID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"SignalName"];
        if (elementArray.count>0) {
            [signal setSignalName:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"CycleLength"];
        if (elementArray.count>0) {
            [signal setCycleLength:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"Offset"];
        if (elementArray.count>0) {
            [signal setOffset:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"Location"];
        if (elementArray.count>0) {
            [signal setLocation:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"Ring1"];
        if (elementArray.count>0) {
            SignalRing *ring1=[[[SignalRing alloc]init] autorelease];
            GDataXMLElement *ring1Element=[elementArray objectAtIndex:0];
            NSArray *ringSubElementArray=[ring1Element elementsForName:@"RingID"];
            if (ringSubElementArray.count>0) {
                [ring1 setRingID:[[ringSubElementArray objectAtIndex:0] stringValue]];
            }
            ringSubElementArray=[ring1Element elementsForName:@"Phases"];
            if (ringSubElementArray.count>0) {
                NSArray *phaseElementArray=[[ringSubElementArray objectAtIndex:0] elementsForName:@"Phase"];
                NSMutableArray *phaseList=[[[NSMutableArray alloc]init]autorelease];
                for (GDataXMLElement *phaseElement in phaseElementArray) {
                    SignalPhase *signalPhase=[[[SignalPhase alloc] init] autorelease];
                    NSArray *phaseSubElementArray=[phaseElement elementsForName:@"PhaseID"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseID:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"PhaseMovement"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseMovement:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"GreenTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setGreenTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"YellowTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setYellowTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"AllRedTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setAllRedTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    [phaseList addObject:signalPhase];
                }
                [ring1 setPhaseList:phaseList];
            }
            [signal setRing1:ring1];
            
        }
        elementArray=[signalElement elementsForName:@"Ring2"];
        if (elementArray.count>0) {
            SignalRing *ring2=[[[SignalRing alloc]init] autorelease];
            GDataXMLElement *ring2Element=[elementArray objectAtIndex:0];
            NSArray *ringSubElementArray=[ring2Element elementsForName:@"RingID"];
            if (ringSubElementArray.count>0) {
                [ring2 setRingID:[[ringSubElementArray objectAtIndex:0] stringValue]];
            }
            ringSubElementArray=[ring2Element elementsForName:@"Phases"];
            if (ringSubElementArray.count>0) {
                NSArray *phaseElementArray=[[ringSubElementArray objectAtIndex:0] elementsForName:@"Phase"];
                NSMutableArray *phaseList=[[[NSMutableArray alloc]init]autorelease];
                for (GDataXMLElement *phaseElement in phaseElementArray) {
                    SignalPhase *signalPhase=[[[SignalPhase alloc] init] autorelease];
                    NSArray *phaseSubElementArray=[phaseElement elementsForName:@"PhaseID"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseID:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"PhaseMovement"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseMovement:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"GreenTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setGreenTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"YellowTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setYellowTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"AllRedTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setAllRedTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    [phaseList addObject:signalPhase];
                }
                [ring2 setPhaseList:phaseList];
            }
            [signal setRing2:ring2];
            
        }
        return signal;
        
        
    }
    return nil;
//    if (signalList.count>0) {
//        return [signalList objectAtIndex:0];
//    }else {
//        return nil;
//    }

}


+ (void)createTimerGroup:(TimerGroup *) timerGroup
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups"];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString1 error:nil];
    if (parentElements.count<=0) {
        return;
    }
    GDataXMLElement *timerGroupElement=[GDataXMLElement elementWithName:@"TimerGroup"];
    GDataXMLElement *timerGroupIDElement=[GDataXMLElement elementWithName:@"TimerGroupID" stringValue:timerGroup.timerGroupID];
    GDataXMLElement *timerGroupSyncTimeElement=[GDataXMLElement elementWithName:@"SyncTime" stringValue:timerGroup.syncTime];
    GDataXMLElement *timerGroupSignalsElement=[GDataXMLElement elementWithName:@"Signals"];
    [timerGroupElement addChild:timerGroupIDElement];
    [timerGroupElement addChild:timerGroupSyncTimeElement];
    [timerGroupElement addChild:timerGroupSignalsElement];
    [[parentElements objectAtIndex:0] addChild:timerGroupElement];
    [self saveDataXML];
    if (timerGroup.signalList!=nil) {
        for (int i=0; i<timerGroup.signalList.count; i++) {
            [XMLParser createSignal:timerGroup.timerGroupID signal:[timerGroup.signalList objectAtIndex:i]];
        }
    }
    
}
+ (void)createSignal:(NSString *) timerGroupID signal:(Signal *)signal
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
    NSString *xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
    
    NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]/Signals"];
    NSLog(@"%@ \n", xPathString3);        
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString3 error:nil];
    
    if (parentElements.count<=0) {
        return;
    }
    GDataXMLElement *signalElement=[GDataXMLElement elementWithName:@"Signal"];
    GDataXMLElement *signalIDElement=[GDataXMLElement elementWithName:@"SignalID" stringValue:signal.signalID];
    GDataXMLElement *signalNameElement=[GDataXMLElement elementWithName:@"SignalName" stringValue:signal.signalName];
    GDataXMLElement *signalCycleLengthElement=[GDataXMLElement elementWithName:@"CycleLength" stringValue:signal.cycleLength];
    GDataXMLElement *signalOffsetElement=[GDataXMLElement elementWithName:@"Offset" stringValue:signal.offset];
    GDataXMLElement *signalLocationElement=[GDataXMLElement elementWithName:@"Location" stringValue:signal.location];
//    {
        GDataXMLElement *signalRing1Element=[GDataXMLElement elementWithName:@"Ring1"];
        GDataXMLElement *signalRing1IDElement=[GDataXMLElement elementWithName:@"RingID" stringValue:@"1"];
        GDataXMLElement *signalRing1PhasesElement=[GDataXMLElement elementWithName:@"Phases"];
        [signalRing1Element addChild:signalRing1IDElement];
            [signalRing1Element addChild:signalRing1PhasesElement];
        
//    }
//    {
        GDataXMLElement *signalRing2Element=[GDataXMLElement elementWithName:@"Ring2"];
        GDataXMLElement *signalRing2IDElement=[GDataXMLElement elementWithName:@"RingID" stringValue:@"2"];
        GDataXMLElement *signalRing2PhasesElement=[GDataXMLElement elementWithName:@"Phases"];
        [signalRing2Element addChild:signalRing2IDElement];
        [signalRing2Element addChild:signalRing2PhasesElement];
//    }
    [signalElement addChild:signalIDElement];
    [signalElement addChild:signalNameElement];
    [signalElement addChild:signalCycleLengthElement];
    [signalElement addChild:signalOffsetElement];
    [signalElement addChild:signalLocationElement];
    [signalElement addChild:signalRing1Element];
    [signalElement addChild:signalRing2Element];
    [[parentElements objectAtIndex:0] addChild:signalElement];
    [self saveDataXML];
    if (signal.ring1.phaseList!=nil) {
        for (int i=0; i<signal.ring1.phaseList.count; i++) {
            [XMLParser createSignalPhase:timerGroupID signalID:signal.signalID signalRingID:@"1" signaPhase:[signal.ring1.phaseList objectAtIndex:i]];
        }
    }
    if (signal.ring2.phaseList!=nil) {
        for (int i=0; i<signal.ring2.phaseList.count; i++) {
            [XMLParser createSignalPhase:timerGroupID signalID:signal.signalID signalRingID:@"2" signaPhase:[signal.ring2.phaseList objectAtIndex:i]];
        }
    }
    
}
+ (void) createSignalRing:(NSString *) timerGroupID signalID:(NSString *) signalID signalRing:(SignalRing *) signalRing
{
    
}
+ (void) createSignalPhase:(NSString *)timerGroupID signalID:(NSString *)signalID signalRingID:(NSString *)signalRingID signaPhase:(SignalPhase *)signalPhase
{
    if ([signalRingID isEqualToString:@"1"]) {
        NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
        NSString *xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
        
        NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]/Signals/Signal[SignalID=\""];
        NSString *xPathString4=[xPathString3 stringByAppendingString:signalID];
        NSString *xPathString5=[xPathString4 stringByAppendingString:@"\"]/Ring1/Phases"];        
        NSArray *parentElements=[dataDoc nodesForXPath:xPathString5 error:nil];
        if (parentElements.count<=0) {
            return;
        }
        
        GDataXMLElement *phaseElement=[GDataXMLElement elementWithName:@"Phase"];
        GDataXMLElement *phaseIDElement=[GDataXMLElement elementWithName:@"PhaseID" stringValue:signalPhase.phaseID];
        GDataXMLElement *phaseMovementElement=[GDataXMLElement elementWithName:@"PhaseMovement" stringValue:signalPhase.phaseMovement];
        GDataXMLElement *phaseGreenTime=[GDataXMLElement elementWithName:@"GreenTime" stringValue:signalPhase.greenTime];
        GDataXMLElement *phaseYellowTime=[GDataXMLElement elementWithName:@"YellowTime" stringValue:signalPhase.yellowTime];
        GDataXMLElement *phaseAllRedTime=[GDataXMLElement elementWithName:@"AllRedTime" stringValue:signalPhase.allRedTime];
        [phaseElement addChild:phaseIDElement];
        [phaseElement addChild:phaseMovementElement];
        [phaseElement addChild:phaseGreenTime];
        [phaseElement addChild:phaseYellowTime];
        [phaseElement addChild:phaseAllRedTime];
        [[parentElements objectAtIndex:0] addChild:phaseElement];
        [self saveDataXML];
    }else {
        NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
        NSString *xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
        
        NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]/Signals/Signal[SignalID=\""];
        NSString *xPathString4=[xPathString3 stringByAppendingString:signalID];
        NSString *xPathString5=[xPathString4 stringByAppendingString:@"\"]/Ring2/Phases"];        
        NSArray *parentElements=[dataDoc nodesForXPath:xPathString5 error:nil];
        if (parentElements.count<=0) {
            return;
        }
        
        GDataXMLElement *phaseElement=[GDataXMLElement elementWithName:@"Phase"];
        GDataXMLElement *phaseIDElement=[GDataXMLElement elementWithName:@"PhaseID" stringValue:signalPhase.phaseID];
        GDataXMLElement *phaseMovementElement=[GDataXMLElement elementWithName:@"PhaseMovement" stringValue:signalPhase.phaseMovement];
        GDataXMLElement *phaseGreenTime=[GDataXMLElement elementWithName:@"GreenTime" stringValue:signalPhase.greenTime];
        GDataXMLElement *phaseYellowTime=[GDataXMLElement elementWithName:@"YellowTime" stringValue:signalPhase.yellowTime];
        GDataXMLElement *phaseAllRedTime=[GDataXMLElement elementWithName:@"AllRedTime" stringValue:signalPhase.allRedTime];
        [phaseElement addChild:phaseIDElement];
        [phaseElement addChild:phaseMovementElement];
        [phaseElement addChild:phaseGreenTime];
        [phaseElement addChild:phaseYellowTime];
        [phaseElement addChild:phaseAllRedTime];
        [[parentElements objectAtIndex:0] addChild:phaseElement];
        [self saveDataXML];

    }
    
}

+(void) removeTimerGroupByID: (NSString *) timerGroupID
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups"];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString1 error:nil];
    if (parentElements.count<=0) {
        return;
    }
    
    xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
    NSString *xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
    
    NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]"];
    NSArray *childElements=[dataDoc nodesForXPath:xPathString3 error:nil];
    if (childElements.count<=0) {
        return;
    }
    for (int i=0; i<childElements.count; i++) {
        [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
    }
    [self saveDataXML];
}
+(void) removeSignalByTimerGroupIDAndSignalID:(NSString *) timerGroupID signalID:(NSString *) signalID
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
    NSString *xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
    
    NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]/Signals"];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString3 error:nil];
    if (parentElements.count<=0) {
        return;
    }
    
    xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
    xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
    
    xPathString3=[xPathString2 stringByAppendingString:@"\"]/Signals/Signal[SignalID=\""];
    NSString *xPathString4=[xPathString3 stringByAppendingString:signalID];
    NSString *xPathString5=[xPathString4 stringByAppendingString:@"\"]"]; 
    NSArray *childElements=[dataDoc nodesForXPath:xPathString5 error:nil];
    if (childElements.count<=0) {
        return;
    }
    for (int i=0; i<childElements.count; i++) {
        [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
    }
    [self saveDataXML];
}
+(void) removePhaseByTimerGroupIDSignalIDAndPhaseID:(NSString*) timerGroupID signalID:(NSString *)signalID ringID:(NSString *)ringID phaseID:(NSString *)phaseID{
    if ([ringID isEqualToString:@"1"]) {
        NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
        NSString *xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
        
        NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]/Signals/Signal[SignalID=\""];
        NSString *xPathString4=[xPathString3 stringByAppendingString:signalID];
        NSString *xPathString5=[xPathString4 stringByAppendingString:@"\"]/Ring1/Phases"];        
        NSArray *parentElements=[dataDoc nodesForXPath:xPathString5 error:nil];
        if (parentElements.count<=0) {
            return;
        }
        
       xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
       xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
        
        xPathString3=[xPathString2 stringByAppendingString:@"\"]/Signals/Signal[SignalID=\""];
        xPathString4=[xPathString3 stringByAppendingString:signalID];
        xPathString5=[xPathString4 stringByAppendingString:@"\"]/Ring1/Phases/Phase[PhaseID=\""];
        NSString *xPathString6=[xPathString5 stringByAppendingString:phaseID];
        NSString *xPathString7=[xPathString6 stringByAppendingString:@"\"]"];
        NSLog(@"%@ \n", xPathString7);
        NSArray *childElements=[dataDoc nodesForXPath:xPathString7 error:nil];
        if (childElements.count<=0) {
            return;
        }
        for (int i=0; i<childElements.count; i++) {
            [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
        }
        [self saveDataXML];
    }else {
        NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
        NSString *xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
        
        NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]/Signals/Signal[SignalID=\""];
        NSString *xPathString4=[xPathString3 stringByAppendingString:signalID];
        NSString *xPathString5=[xPathString4 stringByAppendingString:@"\"]/Ring2/Phases"];        
        NSArray *parentElements=[dataDoc nodesForXPath:xPathString5 error:nil];
        if (parentElements.count<=0) {
            return;
        }
        
        xPathString1=[dataXMLRoot stringByAppendingString:@"/TimerGroups/TimerGroup[TimerGroupID=\""];
        xPathString2=[xPathString1 stringByAppendingString:timerGroupID];
        
        xPathString3=[xPathString2 stringByAppendingString:@"\"]/Signals/Signal[SignalID=\""];
        xPathString4=[xPathString3 stringByAppendingString:signalID];
        xPathString5=[xPathString4 stringByAppendingString:@"\"]/Ring2/Phases/Phase[PhaseID=\""];
        NSString *xPathString6=[xPathString5 stringByAppendingString:phaseID];
        NSString *xPathString7=[xPathString6 stringByAppendingString:@"\"]"];
        
        NSArray *childElements=[dataDoc nodesForXPath:xPathString7 error:nil];
        if (childElements.count<=0) {
            return;
        }
        for (int i=0; i<childElements.count; i++) {
            [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
        }
        [self saveDataXML];
        
    }
}
+(void) updateSignal:(NSString *) timerGroupID signal:(Signal *)signal
{
    if (!signal) {
        return;
    }
    TimerGroup *timerGroup=[self getTimerGroup:timerGroupID];
    NSMutableArray *signalList=[NSMutableArray arrayWithArray:timerGroup.signalList];
    BOOL signalExist=NO;
    for (int i=0; i<signalList.count; i++) {
        Signal *tempSignal=[signalList objectAtIndex:i];
        [self removeSignalByTimerGroupIDAndSignalID:timerGroupID signalID:tempSignal.signalID];
        if ([signal.signalID isEqualToString:tempSignal.signalID]) {
            [signalList replaceObjectAtIndex:i withObject:signal];
            signalExist=YES;
        }
    }
    if (!signalExist) {
        [signalList addObject:signal];
    }
    for (int i=0; i<signalList.count; i++) {
        Signal *tempSignal=[signalList objectAtIndex:i];
        [self createSignal:timerGroupID signal:tempSignal];
    }
    
}
+(void) updatePhase:(NSString *)timerGroupID signalID:(NSString *)signalID signalRingID:(NSString *)signalRingID signaPhase:(SignalPhase *)signalPhase
{
    if (!signalPhase) {
        return;
    }
    Signal *signal=[self getSignalByTimerGroupIDAndSignalID:timerGroupID signalID:signalID];
    SignalRing *ring;
    BOOL signalExist=NO;
    if ([signalRingID isEqualToString:@"1"]) {
        ring=signal.ring1;
        NSMutableArray *phaseList=[NSMutableArray arrayWithArray:ring.phaseList];
        for (int i=0; i<phaseList.count; i++) {
            SignalPhase *tempPhase=[phaseList objectAtIndex:i];
            [self removePhaseByTimerGroupIDSignalIDAndPhaseID:timerGroupID signalID:signalID ringID:signalRingID phaseID:tempPhase.phaseID];
            if ([signalPhase.phaseID isEqualToString:tempPhase.phaseID]) {
                [phaseList replaceObjectAtIndex:i withObject:signalPhase];
                signalExist=YES;
            }
        }
        if (!signalExist) {
            [phaseList addObject:signalPhase];
        }
        for (int i=0; i<phaseList.count; i++) {
            SignalPhase *phase=[phaseList objectAtIndex:i];
            [self createSignalPhase:timerGroupID signalID:signalID signalRingID:signalRingID signaPhase:phase];
        }
        
    }else {
        ring=signal.ring2;
        NSMutableArray *phaseList=[NSMutableArray arrayWithArray:ring.phaseList];
        for (int i=0; i<phaseList.count; i++) {
            SignalPhase *tempPhase=[phaseList objectAtIndex:i];
            [self removePhaseByTimerGroupIDSignalIDAndPhaseID:timerGroupID signalID:signalID ringID:signalRingID phaseID:tempPhase.phaseID];
            if ([signalPhase.phaseID isEqualToString:tempPhase.phaseID]) {
                [phaseList replaceObjectAtIndex:i withObject:signalPhase];
                signalExist=YES;
            }
        }
        if (!signalExist) {
            [phaseList addObject:signalPhase];
        }
        for (int i=0; i<phaseList.count; i++) {
            SignalPhase *phase=[phaseList objectAtIndex:i];
            [self createSignalPhase:timerGroupID signalID:signalID signalRingID:signalRingID signaPhase:phase];
        }

    }
    
}


//Operations of agency
+ (void) createAgency: (Agency *) agency
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/Agencies"];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString1 error:nil];
    if (parentElements.count<=0) {
        return;
    }
    GDataXMLElement *agencyElement=[GDataXMLElement elementWithName:@"Agency"];
    GDataXMLElement *agencyIDElement=[GDataXMLElement elementWithName:@"AgencyID" stringValue:agency.agencyID];
    GDataXMLElement *agencyNameElement=[GDataXMLElement elementWithName:@"AgencyName" stringValue:agency.agencyName];
    GDataXMLElement *syncTimeElement=[GDataXMLElement elementWithName:@"SyncTime" stringValue:@"01:00 AM"];
    GDataXMLElement *baseTimeOffsetElement=[GDataXMLElement elementWithName:@"BaseTimeOffset" stringValue:@"0"];
    GDataXMLElement *subSystemsElement=[GDataXMLElement elementWithName:@"SubSystems"];
    
    [agencyElement addChild:agencyIDElement];
    [agencyElement addChild:agencyNameElement];
    [agencyElement addChild:syncTimeElement];
    [agencyElement addChild:baseTimeOffsetElement];
    [agencyElement addChild:subSystemsElement];
    [[parentElements objectAtIndex:0] addChild:agencyElement];
    [self saveDataXML];
//    if (timerGroup.signalList!=nil) {
//        for (int i=0; i<timerGroup.signalList.count; i++) {
//            [XMLParser createSignal:timerGroup.timerGroupID signal:[timerGroup.signalList objectAtIndex:i]];
//        }
//    }
}
+ (void) removeAgency: (NSString *) agencyID
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/Agencies"];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString1 error:nil];
    if (parentElements.count<=0) {
        return;
    }
    
    xPathString1=[dataXMLRoot stringByAppendingString:@"/Agencies/Agency[AgencyID=\""];
    NSString *xPathString2=[xPathString1 stringByAppendingString:agencyID];
    
    NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]"];
    NSArray *childElements=[dataDoc nodesForXPath:xPathString3 error:nil];
    if (childElements.count<=0) {
        return;
    }
    for (int i=0; i<childElements.count; i++) {
        [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
    }
    [self saveDataXML];
}

+ (NSArray *) getAgencyList
{
    NSString *xPathString=[dataXMLRoot stringByAppendingString:@"/Agencies/Agency"];
    NSLog(@"\n %@!!", xPathString);
    
    NSArray *agencyElementList;
    if (dataDoc!=nil) {
        agencyElementList=[dataDoc nodesForXPath:xPathString error:nil];
    }else {
        return nil;
    }
    NSMutableArray *agencyList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement *agencyElement in agencyElementList) {
        Agency * agency=[[[Agency alloc] init] autorelease];
        NSArray *IDArray=[agencyElement elementsForName:@"AgencyID"];
        if (IDArray.count>0) {
            [agency setAgencyID:[[IDArray objectAtIndex:0] stringValue]];
            
        }
        NSArray * nameArray=[agencyElement elementsForName:@"AgencyName"];
        if (nameArray.count>0) {
            [agency setAgencyName:[[nameArray objectAtIndex:0] stringValue]];
        }
        NSArray * syncTimeArray=[agencyElement elementsForName:@"SyncTime"];
        if (syncTimeArray.count>0) {
            [agency setSyncTime:[[syncTimeArray objectAtIndex:0] stringValue]];
        }
        NSArray * baseTimeOffsetArray=[agencyElement elementsForName:@"BaseTimeOffset"];
        if (baseTimeOffsetArray.count>0) {
            [agency setBaseTimeOffset:[[baseTimeOffsetArray objectAtIndex:0] stringValue]];
        }
        NSArray * subSystemsArray=[agencyElement elementsForName:@"SubSystems"];
        if (subSystemsArray.count>0) {
            NSMutableArray *subSystemIDList=[[[NSMutableArray alloc] init] autorelease];
            GDataXMLElement *subSystemsElement=[subSystemsArray objectAtIndex:0];
            NSArray *subSystemArray=[subSystemsElement elementsForName:@"SubSystem"];
            for (GDataXMLElement *subSystem in subSystemArray) {
                NSString *subSystemID=[[[subSystem elementsForName:@"SubSystemID"] objectAtIndex:0] stringValue];
                [subSystemIDList addObject:subSystemID];
                
            }
            [agency setSubSystemIDList:subSystemIDList];
            
        }
        [agencyList addObject:agency];
        
    }
    return agencyList;
//    return nil;
}
+ (Agency *) getAgencyByAgencyID: (NSString *) agencyID
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/Agencies/Agency[AgencyID=\""];
    NSString *xPathString2=[xPathString1 stringByAppendingString:agencyID];
    
    NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]"];
    NSArray *agencyElementList;
    if (dataDoc!=nil) {
        agencyElementList=[dataDoc nodesForXPath:xPathString3 error:nil];
    }else {
        return nil;
    }

    //NSMutableArray *agencyList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement *agencyElement in agencyElementList) {
        Agency * agency=[[[Agency alloc] init] autorelease];
        NSArray *IDArray=[agencyElement elementsForName:@"AgencyID"];
        if (IDArray.count>0) {
            [agency setAgencyID:[[IDArray objectAtIndex:0] stringValue]];
            
        }
        NSArray * nameArray=[agencyElement elementsForName:@"AgencyName"];
        if (nameArray.count>0) {
            [agency setAgencyName:[[nameArray objectAtIndex:0] stringValue]];
        }
        NSArray * syncTimeArray=[agencyElement elementsForName:@"SyncTime"];
        if (syncTimeArray.count>0) {
            [agency setSyncTime:[[syncTimeArray objectAtIndex:0] stringValue]];
        }
        NSArray * baseTimeOffsetArray=[agencyElement elementsForName:@"BaseTimeOffset"];
        if (baseTimeOffsetArray.count>0) {
            [agency setBaseTimeOffset:[[baseTimeOffsetArray objectAtIndex:0] stringValue]];
        }
        NSArray * subSystemsArray=[agencyElement elementsForName:@"SubSystems"];
        if (subSystemsArray.count>0) {
            NSMutableArray *subSystemIDList=[[[NSMutableArray alloc] init] autorelease];
            GDataXMLElement *subSystemsElement=[subSystemsArray objectAtIndex:0];
            NSArray *subSystemArray=[subSystemsElement elementsForName:@"SubSystem"];
            for (GDataXMLElement *subSystem in subSystemArray) {
                NSString *subSystemID=[[[subSystem elementsForName:@"SubSystemID"] objectAtIndex:0] stringValue];
                [subSystemIDList addObject:subSystemID];
                
            }
            [agency setSubSystemIDList:subSystemIDList];
            
        }
        return agency;
        
    }
    return nil;
}
+ (void) updateAgency: (Agency *) agency
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/Agencies"];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString1 error:nil];
    if (parentElements.count<=0) {
        return;
    }
     NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]",agency.agencyID];
    NSLog(@"\n %@!!", xPathString);
    
    NSArray *childrenElementList;
    childrenElementList=[dataDoc nodesForXPath:xPathString error:nil];
    BOOL elementExist=NO;
    for (GDataXMLElement *childElement in childrenElementList) {
        NSString *childElementID=[[[childElement elementsForName:@"AgencyID"] objectAtIndex:0] stringValue];
        if (![childElementID isEqualToString:agency.agencyID]) {
            continue;
        }
        elementExist=YES;
        GDataXMLElement *childNameElement=[[childElement elementsForName:@"AgencyName"] objectAtIndex:0];
        [childNameElement setStringValue:agency.agencyName];
        GDataXMLElement *syncTimeElement=[[childElement elementsForName:@"SyncTime"] objectAtIndex:0];
        [syncTimeElement setStringValue:agency.syncTime];
        GDataXMLElement *baseTimeOffset=[[childElement elementsForName:@"BaseTimeOffset"] objectAtIndex:0];
        [baseTimeOffset setStringValue:agency.baseTimeOffset];
        
    }
    [self saveDataXML];
    if (!elementExist) {
        [XMLParser createAgency:agency];
    }
}

+ (NSString *) createAgencyID
{
    NSArray * agencyList=[self getAgencyList];
    int maxID=-1;
    for (int i=0; i<agencyList.count; i++) {
        Agency *agency=[agencyList objectAtIndex:i];
        if (maxID<[agency.agencyID intValue]) {
            maxID=[agency.agencyID intValue];
        }
    }
    maxID=maxID+1;
    return [NSString stringWithFormat:@"%d",maxID];
}
//Operations of subSystem
+ (void) createSubSystem:(SubSystem *)subSystem agencyID:(NSString *)agencyID
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/Agencies/Agency[AgencyID=\""];
    NSString *xPathString2=[xPathString1 stringByAppendingString:agencyID];
    
    NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]/SubSystems"];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString3 error:nil];
    if (parentElements.count<=0) {
        return;
    }
    GDataXMLElement *subSystemElement=[GDataXMLElement elementWithName:@"SubSystem"];
    GDataXMLElement *subSystemIDElement=[GDataXMLElement elementWithName:@"SubSystemID" stringValue:subSystem.subSystemID];
    GDataXMLElement *subSystemNameElement=[GDataXMLElement elementWithName:@"SubSystemName" stringValue:subSystem.subSystemName];
    GDataXMLElement *regionElement=[GDataXMLElement elementWithName:@"Region"];
    GDataXMLElement *intersectionsElement=[GDataXMLElement elementWithName:@"Intersections"];
    GDataXMLElement *timingPatternsElement=[GDataXMLElement elementWithName:@"TimingPatterns"];
    GDataXMLElement *linksElement=[GDataXMLElement elementWithName:@"Links"];
    GDataXMLElement *regionCenterElement=[GDataXMLElement elementWithName:@"Center"];
    GDataXMLElement *regionCenterLatitudeElement=[GDataXMLElement elementWithName:@"CenterLatitude" stringValue:@"0"];
    GDataXMLElement *regionCenterLongitudeElement=[GDataXMLElement elementWithName:@"CenterLongitude" stringValue:@"0"];
    [regionCenterElement addChild:regionCenterLatitudeElement];
    [regionCenterElement addChild:regionCenterLongitudeElement];
    [regionElement addChild:regionCenterElement];
    GDataXMLElement *regionRangeElement=[GDataXMLElement elementWithName:@"Range"];
    GDataXMLElement *regionRangeWidthElement=[GDataXMLElement elementWithName:@"RangeWidth" stringValue:@"5000"];
    GDataXMLElement *regionRangeLengthElement=[GDataXMLElement elementWithName:@"RangeLength" stringValue:@"5000"];
    [regionRangeElement addChild:regionRangeWidthElement];
    [regionRangeElement addChild:regionRangeLengthElement];
    [regionElement addChild:regionRangeElement];
    
    
    [subSystemElement addChild:subSystemIDElement];
    [subSystemElement addChild:subSystemNameElement];
    [subSystemElement addChild:regionElement];
    [subSystemElement addChild:intersectionsElement];
    [subSystemElement addChild:linksElement];
    [subSystemElement addChild:timingPatternsElement];
    
    [[parentElements objectAtIndex:0] addChild:subSystemElement];
    [self saveDataXML];
}
+ (void) removeSubSystem:(NSString *)subSystemID agencyID:(NSString *)agencyID
{
    
    NSString *xPathString1=[dataXMLRoot stringByAppendingString:@"/Agencies/Agency[AgencyID=\""];
    NSString *xPathString2=[xPathString1 stringByAppendingString:agencyID];
    
    NSString *xPathString3=[xPathString2 stringByAppendingString:@"\"]/SubSystems"];
    
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString3 error:nil];
    if (parentElements.count<=0) {
        return;
    }
    
    xPathString1=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]",agencyID,subSystemID];
    
    NSArray *childElements=[dataDoc nodesForXPath:xPathString1 error:nil];
    if (childElements.count<=0) {
        return;
    }
    for (int i=0; i<childElements.count; i++) {
        [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
    }
    [self saveDataXML];
}
+ (NSArray *) getSubSystemList:(NSString *)agencyID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem",agencyID];
    NSLog(@"\n %@!!", xPathString);
    
    NSArray *resultElementList;
    if (dataDoc!=nil) {
        resultElementList=[dataDoc nodesForXPath:xPathString error:nil];
    }else {
        return nil;
    }
    NSMutableArray *resultList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement *resultElement in resultElementList) {
        SubSystem * result=[[[SubSystem alloc] init] autorelease];
        if ([[resultElement elementsForName:@"SubSystemID"] count]>0) {
            [result setSubSystemID:[[[resultElement elementsForName:@"SubSystemID"] objectAtIndex:0] stringValue]];
        }
        
        if ([[resultElement elementsForName:@"SubSystemName"] count]>0) {
            [result setSubSystemName:[[[resultElement elementsForName:@"SubSystemName"] objectAtIndex:0] stringValue]];
        }
        NSArray * regionElementsArray=[resultElement elementsForName:@"Region"];
        CLLocationCoordinate2D center={0,0};
        result.region=MKCoordinateRegionMakeWithDistance(center,5000,5000);
        if (regionElementsArray.count>0) {
            float centerLatitude=0;
            float centerLongitude=0;
            float width=5000;
            float length=5000;
            GDataXMLElement *regionElement=[regionElementsArray objectAtIndex:0];
            if ([[regionElement elementsForName:@"Center"] count]>0) {
                GDataXMLElement *regionCenterElement=[[regionElement elementsForName:@"Center"] objectAtIndex:0];
                if ([[regionCenterElement elementsForName:@"CenterLatitude"] count]>0) {
                    centerLatitude=[[[[regionCenterElement elementsForName:@"CenterLatitude"] objectAtIndex:0] stringValue] floatValue];
                }
                if ([[regionCenterElement elementsForName:@"CenterLongitude"] count]>0) {
                    centerLongitude=[[[[regionCenterElement elementsForName:@"CenterLongitude"] objectAtIndex:0] stringValue] floatValue];
                }
            }
            if ([[regionElement elementsForName:@"Range"] count]>0) {
                GDataXMLElement *regionRangeElement=[[regionElement elementsForName:@"Range"] objectAtIndex:0];
                if ([[regionRangeElement elementsForName:@"RangeWidth"] count]>0) {
                    width=[[[[regionRangeElement elementsForName:@"RangeWidth"] objectAtIndex:0] stringValue] floatValue];
                    NSLog(@"\n%@",[[[regionRangeElement elementsForName:@"RangeWidth"] objectAtIndex:0] stringValue]);
                }
                if ([[regionRangeElement elementsForName:@"RangeLength"] count]>0) {
                    length=[[[[regionRangeElement elementsForName:@"RangeLength"] objectAtIndex:0] stringValue] floatValue];
                    NSLog(@"\n%@",[[[regionRangeElement elementsForName:@"RangeLength"] objectAtIndex:0] stringValue]);
                }
            }
            CLLocationCoordinate2D c={centerLatitude,centerLongitude};
            MKCoordinateSpan s={length,width};
            result.region=MKCoordinateRegionMake(c, s);
        }
        NSArray * subElementsArray=[resultElement elementsForName:@"TimingPatterns"];
        if (subElementsArray.count>0) {
            NSMutableArray *subElementIDList=[[[NSMutableArray alloc] init] autorelease];
            GDataXMLElement *subElement=[subElementsArray objectAtIndex:0];
            NSArray *subIDElementArray=[subElement elementsForName:@"TimingPattern"];
            for (GDataXMLElement *subIDElement in subIDElementArray) {
                NSString *subID=[[[subIDElement elementsForName:@"TimingPatternID"] objectAtIndex:0] stringValue];
                [subElementIDList addObject:subID];
                
            }
            [result setTimingPatternIDList:subElementIDList];
            
        }
        [resultList addObject:result];
        
    }
    return resultList;
}
+ (SubSystem *) getSubSystem:(NSString *)subSystemID agencyID:(NSString *)agencyID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]",agencyID,subSystemID];
    NSLog(@"\n %@!!", xPathString);
    
    NSArray *resultElementList;
    if (dataDoc!=nil) {
        resultElementList=[dataDoc nodesForXPath:xPathString error:nil];
    }else {
        return nil;
    }
    for (GDataXMLElement *resultElement in resultElementList) {
        SubSystem * result=[[[SubSystem alloc] init] autorelease];
        if ([[resultElement elementsForName:@"SubSystemID"] count]>0) {
            [result setSubSystemID:[[resultElement elementsForName:@"SubSystemID"] objectAtIndex:0]];
        }
        
        if ([[resultElement elementsForName:@"SubSystemName"] count]>0) {
            [result setSubSystemName:[[resultElement elementsForName:@"SubSystemName"] objectAtIndex:0]];
        }
        
        NSArray * regionElementsArray=[resultElement elementsForName:@"Region"];
        CLLocationCoordinate2D center={0,0};
        result.region=MKCoordinateRegionMakeWithDistance(center,5000,5000);
        if (regionElementsArray.count>0) {
            double centerLatitude=0;
            double centerLongitude=0;
            double width=5000;
            double length=5000;
            GDataXMLElement *regionElement=[regionElementsArray objectAtIndex:0];
            if ([[regionElement elementsForName:@"Center"] count]>0) {
                GDataXMLElement *regionCenterElement=[[regionElement elementsForName:@"Center"] objectAtIndex:0];
                if ([[regionCenterElement elementsForName:@"CenterLatitude"] count]>0) {
                    centerLatitude=[[[[regionCenterElement elementsForName:@"CenterLatitude"] objectAtIndex:0] stringValue] floatValue];
                }
                if ([[regionCenterElement elementsForName:@"CenterLongitude"] count]>0) {
                    centerLongitude=[[[[regionCenterElement elementsForName:@"CenterLongitude"] objectAtIndex:0] stringValue] floatValue];
                }
            }
            if ([[regionElement elementsForName:@"Range"] count]>0) {
                GDataXMLElement *regionRangeElement=[[regionElement elementsForName:@"Range"] objectAtIndex:0];
                if ([[regionRangeElement elementsForName:@"RangeWidth"] count]>0) {
                    width=[[[[regionRangeElement elementsForName:@"RangeWidth"] objectAtIndex:0] stringValue] floatValue];
                }
                if ([[regionRangeElement elementsForName:@"RangeLength"] count]>0) {
                    length=[[[[regionRangeElement elementsForName:@"RangeLength"] objectAtIndex:0] stringValue] floatValue];
                }
            }
            CLLocationCoordinate2D c={centerLatitude,centerLongitude};
            MKCoordinateSpan s={length,width};
            result.region=MKCoordinateRegionMake(c, s);
        }

        NSArray * subElementsArray=[resultElement elementsForName:@"TimingPatterns"];
        if (subElementsArray.count>0) {
            NSMutableArray *subElementIDList=[[[NSMutableArray alloc] init] autorelease];
            GDataXMLElement *subElement=[subElementsArray objectAtIndex:0];
            NSArray *subIDElementArray=[subElement elementsForName:@"TimingPattern"];
            for (GDataXMLElement *subIDElement in subIDElementArray) {
                NSString *subID=[[[subIDElement elementsForName:@"TimingPatternID"] objectAtIndex:0] stringValue];
                [subElementIDList addObject:subID];
                
            }
            [result setTimingPatternIDList:subElementIDList];
            
        }
        return result;
        
    }
    return nil;
}

+(void) updateSubSystem: (SubSystem *) subSystem agencyID:(NSString *)agencyID
{
    NSString *xPathString1=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem",agencyID];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString1 error:nil];
    if (parentElements.count<=0) {
        return;
    }
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]",agencyID,subSystem.subSystemID];
    NSLog(@"\n %@!!", xPathString);
    
    NSArray *childrenElementList;
    childrenElementList=[dataDoc nodesForXPath:xPathString error:nil];
    BOOL elementExist=NO;
    for (GDataXMLElement *childElement in childrenElementList) {
        NSString *childElementID=[[[childElement elementsForName:@"SubSystemID"] objectAtIndex:0] stringValue];
        if (![childElementID isEqualToString:subSystem.subSystemID]) {
            continue;
        }
        elementExist=YES;
        GDataXMLElement *childNameElement=[[childElement elementsForName:@"SubSystemName"] objectAtIndex:0];
        [childNameElement setStringValue:subSystem.subSystemName];
        if ([[childElement elementsForName:@"Region"] count]>0) {
            GDataXMLElement *regionElement=[[childElement elementsForName:@"Region"] objectAtIndex:0];
            [childElement removeChild:regionElement];
            
            
        }
        {
            GDataXMLElement *regionElement=[GDataXMLElement elementWithName:@"Region"];
            GDataXMLElement *regionCenterElement=[GDataXMLElement elementWithName:@"Center"];
            GDataXMLElement *regionCenterLatitudeElement=[GDataXMLElement elementWithName:@"CenterLatitude" stringValue:[NSString stringWithFormat:@"%f",subSystem.region.center.latitude]];
            GDataXMLElement *regionCenterLongitudeElement=[GDataXMLElement elementWithName:@"CenterLongitude" stringValue:[NSString stringWithFormat:@"%f",subSystem.region.center.longitude]];
            [regionCenterElement addChild:regionCenterLatitudeElement];
            [regionCenterElement addChild:regionCenterLongitudeElement];
            [regionElement addChild:regionCenterElement];
            GDataXMLElement *regionRangeElement=[GDataXMLElement elementWithName:@"Range"];
            GDataXMLElement *regionRangeWidthElement=[GDataXMLElement elementWithName:@"RangeWidth" stringValue:[NSString stringWithFormat:@"%f",subSystem.region.span.longitudeDelta]];
            GDataXMLElement *regionRangeLengthElement=[GDataXMLElement elementWithName:@"RangeLength" stringValue:[NSString stringWithFormat:@"%f",subSystem.region.span.latitudeDelta]];
            [regionRangeElement addChild:regionRangeWidthElement];
            [regionRangeElement addChild:regionRangeLengthElement];
            [regionElement addChild:regionRangeElement];
            
            [childElement addChild:regionElement];
        }
        
    }
    [self saveDataXML];
    if (!elementExist) {
        [XMLParser createSubSystem:subSystem agencyID:agencyID];
    }
}

+ (NSString *) createSubSystemID:(NSString *)agencyID
{
    NSArray * elementList=[self getSubSystemList:agencyID];
    int maxID=-1;
    for (int i=0; i<elementList.count; i++) {
        SubSystem *element=[elementList objectAtIndex:i];
        if (maxID<[element.subSystemID intValue]) {
            maxID=[element.subSystemID intValue];
        }
    }
    maxID=maxID+1;
    return [NSString stringWithFormat:@"%d",maxID];
}

//Operations of timingPattern
+ (void) createTimingPattern:(TimingPattern *)timingPattern agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns",agencyID,subSystemID];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
    if (parentElements.count<=0) {
        return;
    }
    GDataXMLElement *timingPatternElement=[GDataXMLElement elementWithName:@"TimingPattern"];
    GDataXMLElement *timingPatternIDElement=[GDataXMLElement elementWithName:@"TimingPatternID" stringValue:timingPattern.timingPatternID];
    GDataXMLElement *timingPatternName=[GDataXMLElement elementWithName:@"TimingPatternName" stringValue:timingPattern.timingPatternName];
    GDataXMLElement *cycleLengthElement=[GDataXMLElement elementWithName:@"TimingPatternCycleLength" stringValue:@"100"];
    GDataXMLElement *signalsElement=[GDataXMLElement elementWithName:@"Signals"];
    
    GDataXMLElement *timeRangesElement=[GDataXMLElement elementWithName:@"TimeRanges"];
    NSMutableArray *timeRangeList=timingPattern.timeRangeList;
    for (int i=0; i<timeRangeList.count; i++) {
        TimeRange *timeRange=[timeRangeList objectAtIndex:i];
        GDataXMLElement *timeRangeElement=[GDataXMLElement elementWithName:@"TimeRange"];
        GDataXMLElement *beginTimeElement=[GDataXMLElement elementWithName:@"BeginTime" stringValue:timeRange.beginTime];
        GDataXMLElement *endTimeElement=[GDataXMLElement elementWithName:@"EndTime" stringValue:timeRange.endTime];
        [timeRangeElement addChild:beginTimeElement];
        [timeRangeElement addChild:endTimeElement];
        [timeRangesElement addChild:timeRangeElement];
    }
    
    
    [timingPatternElement addChild:timingPatternIDElement];
    [timingPatternElement addChild:timingPatternName];
    [timingPatternElement addChild:cycleLengthElement];
    [timingPatternElement addChild:timeRangesElement];
    [timingPatternElement addChild:signalsElement];
//    [timingPatternElement addChild:linksElement];
    [[parentElements objectAtIndex:0] addChild:timingPatternElement];
    NSArray * intersectionList=[self getIntersectionList:agencyID subSystemID:subSystemID];
    for (Intersection *intersection in intersectionList) {
        Signal *signal=[[[NEMASignal alloc] initWithCycleLength:timingPattern.cycleLength] autorelease];
        [signal setSignalID:intersection.intersectionID];
        //    [signal setCycleLength:self.timingPattern.cycleLength];
        [signal setOffset:@"0"];
        [signal setSignalName:intersection.intersectionName];
        [XMLParser createSignal:signal agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPattern.timingPatternID];
    }
    [self saveDataXML];
}
+ (void) duplicateTimingPattern:(TimingPattern *)timingPattern originalTimingPatternID:(NSString *)originalTimingPatternID agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns",agencyID,subSystemID];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
    if (parentElements.count<=0) {
        return;
    }
    GDataXMLElement *timingPatternElement=[GDataXMLElement elementWithName:@"TimingPattern"];
    GDataXMLElement *timingPatternIDElement=[GDataXMLElement elementWithName:@"TimingPatternID" stringValue:timingPattern.timingPatternID];
    GDataXMLElement *timingPatternName=[GDataXMLElement elementWithName:@"TimingPatternName" stringValue:timingPattern.timingPatternName];
    GDataXMLElement *cycleLengthElement=[GDataXMLElement elementWithName:@"TimingPatternCycleLength" stringValue:timingPattern.cycleLength];
    GDataXMLElement *signalsElement=[GDataXMLElement elementWithName:@"Signals"];
    
    GDataXMLElement *timeRangesElement=[GDataXMLElement elementWithName:@"TimeRanges"];
    NSMutableArray *timeRangeList=timingPattern.timeRangeList;
    for (int i=0; i<timeRangeList.count; i++) {
        TimeRange *timeRange=[timeRangeList objectAtIndex:i];
        GDataXMLElement *timeRangeElement=[GDataXMLElement elementWithName:@"TimeRange"];
        GDataXMLElement *beginTimeElement=[GDataXMLElement elementWithName:@"BeginTime" stringValue:timeRange.beginTime];
        GDataXMLElement *endTimeElement=[GDataXMLElement elementWithName:@"EndTime" stringValue:timeRange.endTime];
        [timeRangeElement addChild:beginTimeElement];
        [timeRangeElement addChild:endTimeElement];
        [timeRangesElement addChild:timeRangeElement];
    }
    
    
    [timingPatternElement addChild:timingPatternIDElement];
    [timingPatternElement addChild:timingPatternName];
    [timingPatternElement addChild:cycleLengthElement];
    [timingPatternElement addChild:timeRangesElement];
    [timingPatternElement addChild:signalsElement];
    //    [timingPatternElement addChild:linksElement];
    [[parentElements objectAtIndex:0] addChild:timingPatternElement];
    NSArray * signalList=[self getSignalList:agencyID subSystemID:subSystemID timingPatternID:originalTimingPatternID];
    for (int n=0; n<signalList.count; n++) {
        Signal *signal=[signalList objectAtIndex:n];
        [XMLParser createSignal:signal agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPattern.timingPatternID];
    }
//    NSArray * intersectionList=[self getIntersectionList:agencyID subSystemID:subSystemID];
//    for (Intersection *intersection in intersectionList) {
//        Signal *signal=nil;
//        signal=[self getSignal:intersection.intersectionID agencyID:agencyID subSystemID:subSystemID timingPatternID:originalTimingPatternID];
//        if (!signal) {
//            signal=[[[NEMASignal alloc] initWithCycleLength:timingPattern.cycleLength] autorelease];
//            [signal setSignalID:intersection.intersectionID];
//            //    [signal setCycleLength:self.timingPattern.cycleLength];
//            [signal setOffset:@"0"];
//            [signal setSignalName:intersection.intersectionName];
//        }
//        
//        [XMLParser createSignal:signal agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPattern.timingPatternID];
//    }
    [self saveDataXML];
}
+ (void) removeTimingPattern:(NSString *)timingPatternID agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns",agencyID,subSystemID];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
    if (parentElements.count<=0) {
        return;
    }
    
    xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]",agencyID,subSystemID,timingPatternID];
    
    NSArray *childElements=[dataDoc nodesForXPath:xPathString error:nil];
    if (childElements.count<=0) {
        return;
    }
    for (int i=0; i<childElements.count; i++) {
        [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
    }
    [self saveDataXML];
}
+ (NSArray *) getTimingPatternList:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern",agencyID,subSystemID];
    NSLog(@"\n %@!!", xPathString);
    
    NSArray *resultElementList;
    if (dataDoc!=nil) {
        resultElementList=[dataDoc nodesForXPath:xPathString error:nil];
    }else {
        return nil;
    }
    NSMutableArray *resultList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement *resultElement in resultElementList) {
        TimingPattern * result=[[[TimingPattern alloc] init] autorelease];
        if ([[resultElement elementsForName:@"TimingPatternID"] count]>0) {
            [result setTimingPatternID:[[[resultElement elementsForName:@"TimingPatternID"] objectAtIndex:0] stringValue]];
        }
        
        if ([[resultElement elementsForName:@"TimingPatternName"] count]>0) {
            [result setTimingPatternName:[[[resultElement elementsForName:@"TimingPatternName"] objectAtIndex:0] stringValue]];
        }
        
        if ([[resultElement elementsForName:@"TimingPatternCycleLength"] count]>0) {
            [result setCycleLength:[[[resultElement elementsForName:@"TimingPatternCycleLength"] objectAtIndex:0] stringValue]];
        }
        NSArray * subElementsArray=[resultElement elementsForName:@"Signals"];
        if (subElementsArray.count>0) {
            NSMutableArray *subElementIDList=[[[NSMutableArray alloc] init] autorelease];
            GDataXMLElement *subElement=[subElementsArray objectAtIndex:0];
            NSArray *subIDElementArray=[subElement elementsForName:@"Signal"];
            for (GDataXMLElement *subIDElement in subIDElementArray) {
                NSString *subID=[[[subIDElement elementsForName:@"SignalID"] objectAtIndex:0] stringValue];
                [subElementIDList addObject:subID];
                
            }
            [result setSignalIDList:subElementIDList];
            
        }
        
        subElementsArray=[resultElement elementsForName:@"TimeRanges"];
        if (subElementsArray.count>0) {
            NSMutableArray *subElementList=[[[NSMutableArray alloc] init] autorelease];
            GDataXMLElement *subElement=[subElementsArray objectAtIndex:0];
            NSArray *subIDElementArray=[subElement elementsForName:@"TimeRange"];
            for (GDataXMLElement *subIDElement in subIDElementArray) {
                NSString *beginTime=[[[subIDElement elementsForName:@"BeginTime"] objectAtIndex:0] stringValue];
                NSString *endTime=[[[subIDElement elementsForName:@"EndTime"] objectAtIndex:0] stringValue];
                TimeRange * timeRange=[[[TimeRange alloc] init] autorelease];
                [timeRange setBeginTime:beginTime];
                [timeRange setEndTime:endTime];
                [subElementList addObject:timeRange];
                
            }
            [result setTimeRangeList:subElementList];
            
        }
        [resultList addObject:result];
        
    }
    return resultList;
}
+ (TimingPattern *) getTimingPattern:(NSString *)timingPatternID agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]",agencyID,subSystemID,timingPatternID];
    NSLog(@"\n %@!!", xPathString);
    
    NSArray *resultElementList;
    if (dataDoc!=nil) {
        resultElementList=[dataDoc nodesForXPath:xPathString error:nil];
    }else {
        return nil;
    }
    for (GDataXMLElement *resultElement in resultElementList) {
        TimingPattern * result=[[[TimingPattern alloc] init] autorelease];
        if ([[resultElement elementsForName:@"TimingPatternID"] count]>0) {
            [result setTimingPatternID:[[[resultElement elementsForName:@"TimingPatternID"] objectAtIndex:0] stringValue]];
        }
        
        if ([[resultElement elementsForName:@"TimingPatternName"] count]>0) {
            [result setTimingPatternName:[[[resultElement elementsForName:@"TimingPatternName"] objectAtIndex:0] stringValue]];
        }
        
        if ([[resultElement elementsForName:@"TimingPatternCycleLength"] count]>0) {
            [result setCycleLength:[[[resultElement elementsForName:@"TimingPatternCycleLength"] objectAtIndex:0] stringValue]];
        }
        NSArray * subElementsArray=[resultElement elementsForName:@"Signals"];
        if (subElementsArray.count>0) {
            NSMutableArray *subElementIDList=[[[NSMutableArray alloc] init] autorelease];
            GDataXMLElement *subElement=[subElementsArray objectAtIndex:0];
            NSArray *subIDElementArray=[subElement elementsForName:@"Signal"];
            for (GDataXMLElement *subIDElement in subIDElementArray) {
                NSString *subID=[[[subIDElement elementsForName:@"SignalID"] objectAtIndex:0] stringValue];
                [subElementIDList addObject:subID];
                
            }
            [result setSignalIDList:subElementIDList];
            
        }
        
        subElementsArray=[resultElement elementsForName:@"TimeRanges"];
        if (subElementsArray.count>0) {
            NSMutableArray *subElementList=[[[NSMutableArray alloc] init] autorelease];
            GDataXMLElement *subElement=[subElementsArray objectAtIndex:0];
            NSArray *subIDElementArray=[subElement elementsForName:@"TimeRange"];
            for (GDataXMLElement *subIDElement in subIDElementArray) {
                NSString *beginTime=[[[subIDElement elementsForName:@"BeginTime"] objectAtIndex:0] stringValue];
                NSString *endTime=[[[subIDElement elementsForName:@"EndTime"] objectAtIndex:0] stringValue];
                TimeRange * timeRange=[[[TimeRange alloc] init] autorelease];
                [timeRange setBeginTime:beginTime];
                [timeRange setEndTime:endTime];
                [subElementList addObject:timeRange];
                
            }
            [result setTimeRangeList:subElementList];
            
        }
        return result;
        
    }
    return nil;

}

+ (void) updateTimingPattern:(TimingPattern *)timingPattern agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns",agencyID,subSystemID];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
    if (parentElements.count<=0) {
        return;
    }
    
    xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]",agencyID,subSystemID,timingPattern.timingPatternID];
    
    NSLog(@"\n %@!!", xPathString);
    
    NSArray *childrenElementList;
    childrenElementList=[dataDoc nodesForXPath:xPathString error:nil];
    BOOL elementExist=NO;
    for (GDataXMLElement *childElement in childrenElementList) {
        NSString *childElementID=[[[childElement elementsForName:@"TimingPatternID"] objectAtIndex:0] stringValue];
        if (![childElementID isEqualToString:timingPattern.timingPatternID]) {
            continue;
        }
        elementExist=YES;
        GDataXMLElement *childNameElement=[[childElement elementsForName:@"TimingPatternName"] objectAtIndex:0];
        [childNameElement setStringValue:timingPattern.timingPatternName];
        
        GDataXMLElement *childCycleLengthElement=[[childElement elementsForName:@"TimingPatternCycleLength"] objectAtIndex:0];
        [childCycleLengthElement setStringValue:timingPattern.cycleLength];
        
        GDataXMLElement *timeRangesElement=[[childElement elementsForName:@"TimeRanges"] objectAtIndex:0];
        NSArray *timeRangeArray=[timeRangesElement elementsForName:@"TimeRange"];
        for (int i=0; i<timeRangeArray.count; i++) {
            [timeRangesElement removeChild:[timeRangeArray objectAtIndex:i]];
        }
        NSMutableArray *timeRangeList=timingPattern.timeRangeList;
        for (int i=0; i<timeRangeList.count; i++) {
            TimeRange *timeRange=[timeRangeList objectAtIndex:i];
            GDataXMLElement *timeRangeElement=[GDataXMLElement elementWithName:@"TimeRange"];
            GDataXMLElement *beginTimeElement=[GDataXMLElement elementWithName:@"BeginTime" stringValue:timeRange.beginTime];
            GDataXMLElement *endTimeElement=[GDataXMLElement elementWithName:@"EndTime" stringValue:timeRange.endTime];
            [timeRangeElement addChild:beginTimeElement];
            [timeRangeElement addChild:endTimeElement];
            [timeRangesElement addChild:timeRangeElement];
        }
        
//        [self saveDataXML];
        
        
    }
    [self saveDataXML];
    if (!elementExist) {
        [XMLParser createTimingPattern:timingPattern agencyID:agencyID subSystemID:subSystemID];
    }
}

+ (NSString *) createTimingPatternID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSArray * elementList=[self getTimingPatternList:agencyID subSystemID:subSystemID];
    int maxID=-1;
    for (int i=0; i<elementList.count; i++) {
        TimingPattern *element=[elementList objectAtIndex:i];
        if (maxID<[element.timingPatternID intValue]) {
            maxID=[element.timingPatternID intValue];
        }
    }
    maxID=maxID+1;
    return [NSString stringWithFormat:@"%d",maxID];
}



//Operations of signals
+ (void)createSignal:(Signal *) signal agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID timingPatternID:(NSString *)timingPatternID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals",agencyID,subSystemID,timingPatternID];      
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
    
    if (parentElements.count<=0) {
        return;
    }
    Signal * existingSignal=[self getSignal:signal.signalID agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPatternID];
    if (existingSignal) {
        return;
    }
    GDataXMLElement *signalElement=[GDataXMLElement elementWithName:@"Signal"];
    GDataXMLElement *signalIDElement=[GDataXMLElement elementWithName:@"SignalID" stringValue:signal.signalID];
    GDataXMLElement *signalNameElement=[GDataXMLElement elementWithName:@"SignalName" stringValue:signal.signalName];
//    GDataXMLElement *signalCoordinateElement=[GDataXMLElement elementWithName:@"Coordinate"];
//    GDataXMLElement *coordinateLatitudeElement=[GDataXMLElement elementWithName:@"Latitude" stringValue:[NSString stringWithFormat:@"%f",signal.coordinate.latitude]];
//    GDataXMLElement *coordinateLongitudeElement=[GDataXMLElement elementWithName:@"Longitude" stringValue:[NSString stringWithFormat:@"%f",signal.coordinate.longitude]];
//    [signalCoordinateElement addChild:coordinateLatitudeElement];
//    [signalCoordinateElement addChild:coordinateLongitudeElement];
    GDataXMLElement *signalCycleLengthElement=[GDataXMLElement elementWithName:@"CycleLength" stringValue:signal.cycleLength];
    GDataXMLElement *signalOffsetElement=[GDataXMLElement elementWithName:@"Offset" stringValue:signal.offset];
    GDataXMLElement *signalOffsetPhaseElement=[GDataXMLElement elementWithName:@"OffsetPhase" stringValue:signal.offsetPhase];
    GDataXMLElement *signalOffsetBorEElement=nil;
    if (!signal.offsetBorE||[signal.offsetBorE isEqualToString:@""]) {
        signalOffsetBorEElement=[GDataXMLElement elementWithName:@"OffsetBorE" stringValue:@"Green Begin"];

    }else {
        signalOffsetBorEElement=[GDataXMLElement elementWithName:@"OffsetBorE" stringValue:signal.offsetBorE];
    }
    
    
    GDataXMLElement *signalLocationElement=[GDataXMLElement elementWithName:@"Location" stringValue:signal.location];
    //    {
    GDataXMLElement *signalRing1Element=[GDataXMLElement elementWithName:@"Ring1"];
    GDataXMLElement *signalRing1IDElement=[GDataXMLElement elementWithName:@"RingID" stringValue:@"1"];
    GDataXMLElement *signalRing1PhasesElement=[GDataXMLElement elementWithName:@"Phases"];
    [signalRing1Element addChild:signalRing1IDElement];
    [signalRing1Element addChild:signalRing1PhasesElement];
    
    //    }
    //    {
    GDataXMLElement *signalRing2Element=[GDataXMLElement elementWithName:@"Ring2"];
    GDataXMLElement *signalRing2IDElement=[GDataXMLElement elementWithName:@"RingID" stringValue:@"2"];
    GDataXMLElement *signalRing2PhasesElement=[GDataXMLElement elementWithName:@"Phases"];
    [signalRing2Element addChild:signalRing2IDElement];
    [signalRing2Element addChild:signalRing2PhasesElement];
    //    }
    [signalElement addChild:signalIDElement];
    [signalElement addChild:signalNameElement];
//    [signalElement addChild:signalCoordinateElement];
    [signalElement addChild:signalCycleLengthElement];
    [signalElement addChild:signalOffsetElement];
    [signalElement addChild:signalOffsetPhaseElement];
    [signalElement addChild:signalOffsetBorEElement];
    [signalElement addChild:signalLocationElement];
    [signalElement addChild:signalRing1Element];
    [signalElement addChild:signalRing2Element];
    [[parentElements objectAtIndex:0] addChild:signalElement];
    [self saveDataXML];
    
    for (int i=0; i<signal.ring1.phaseList.count; i++) {
        [self createPhase:[signal.ring1.phaseList objectAtIndex:i] agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPatternID signalID:signal.signalID signalRingID:@"1"];
    }
    
    for (int i=0; i<signal.ring2.phaseList.count; i++) {
        [self createPhase:[signal.ring2.phaseList objectAtIndex:i] agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPatternID signalID:signal.signalID signalRingID:@"2"];
    }
//    if (signal.ring1.phaseList!=nil) {
//        for (int i=0; i<signal.ring1.phaseList.count; i++) {
////            [XMLParser createSignalPhase:timerGroupID signalID:signal.signalID signalRingID:@"1" signaPhase:[signal.ring1.phaseList objectAtIndex:i]];
//        }
//    }
//    if (signal.ring2.phaseList!=nil) {
//        for (int i=0; i<signal.ring2.phaseList.count; i++) {
////            [XMLParser createSignalPhase:timerGroupID signalID:signal.signalID signalRingID:@"2" signaPhase:[signal.ring2.phaseList objectAtIndex:i]];
//        }
//    }
    

}
+ (void) removeSignal:(NSString *) signalID agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID timingPatternID:(NSString *)timingPatternID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals",agencyID,subSystemID,timingPatternID];  
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
    if (parentElements.count<=0) {
        return;
    }
    
    xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]",agencyID,subSystemID,timingPatternID,signalID];  
    
    NSArray *childElements=[dataDoc nodesForXPath:xPathString error:nil];
    if (childElements.count<=0) {
        return;
    }
    for (int i=0; i<childElements.count; i++) {
        [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
    }
    [self saveDataXML];
}
+ (NSArray *) getSignalList:(NSString *)agencyID subSystemID:(NSString *)subSystemID timingPatternID:(NSString *)timingPatternID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal",agencyID,subSystemID,timingPatternID];
    NSLog(@"%@",xPathString);
    NSArray *signalElementList=[dataDoc nodesForXPath:xPathString error:nil];
    
    NSMutableArray * signalList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement * signalElement in signalElementList) {
        Signal *signal=[[[Signal alloc] init] autorelease];
        NSArray *elementArray=[signalElement elementsForName:@"SignalID"];
        if (elementArray.count>0) {
            [signal setSignalID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"SignalName"];
        if (elementArray.count>0) {
            [signal setSignalName:[[elementArray objectAtIndex:0] stringValue]];
        }
//        elementArray=[signalElement elementsForName:@"Coordinate"];
//        if (elementArray.count>0) {
//            GDataXMLElement *coordinateElement=[elementArray objectAtIndex:0];
//            elementArray=[coordinateElement elementsForName:@"Latitude"];
//            double latitude=0;
//            if (elementArray.count>0) {
//                latitude=[[[elementArray objectAtIndex:0] stringValue] floatValue];
//            }
//            elementArray=[coordinateElement elementsForName:@"Longitude"];
//            double longitude=0;
//            if (elementArray.count>0) {
//                longitude=[[[elementArray objectAtIndex:0] stringValue] floatValue];
//            }
//            CLLocationCoordinate2D coordinate={latitude,longitude};
//            [signal setCoordinate:coordinate];
//
//        }
        elementArray=[signalElement elementsForName:@"CycleLength"];
        if (elementArray.count>0) {
            [signal setCycleLength:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"Offset"];
        if (elementArray.count>0) {
            [signal setOffset:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"OffsetPhase"];
        if (elementArray.count>0) {
            [signal setOffsetPhase:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"OffsetBorE"];
        if (elementArray.count>0) {
            [signal setOffsetBorE:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"Location"];
        if (elementArray.count>0) {
            [signal setLocation:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"Ring1"];
        if (elementArray.count>0) {
            SignalRing *ring1=[[[SignalRing alloc]init] autorelease];
            GDataXMLElement *ring1Element=[elementArray objectAtIndex:0];
            NSArray *ringSubElementArray=[ring1Element elementsForName:@"RingID"];
            if (ringSubElementArray.count>0) {
                [ring1 setRingID:[[ringSubElementArray objectAtIndex:0] stringValue]];
            }
            ringSubElementArray=[ring1Element elementsForName:@"Phases"];
            if (ringSubElementArray.count>0) {
                NSArray *phaseElementArray=[[ringSubElementArray objectAtIndex:0] elementsForName:@"Phase"];
                NSMutableArray *phaseList=[[[NSMutableArray alloc]init]autorelease];
                for (GDataXMLElement *phaseElement in phaseElementArray) {
                    SignalPhase *signalPhase=[[[SignalPhase alloc] init] autorelease];
                    NSArray *phaseSubElementArray=[phaseElement elementsForName:@"PhaseID"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseID:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"PhaseMovement"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseMovement:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"GreenTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setGreenTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"YellowTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setYellowTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"AllRedTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setAllRedTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"Barrier"];
                    if (phaseSubElementArray) {
                        [signalPhase setBarrier:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"Position"];
                    if (phaseSubElementArray) {
                        [signalPhase setPosition:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    [phaseList addObject:signalPhase];
                }
                [ring1 setPhaseList:phaseList];
            }
            [signal setRing1:ring1];
            
        }
        elementArray=[signalElement elementsForName:@"Ring2"];
        if (elementArray.count>0) {
            SignalRing *ring2=[[[SignalRing alloc]init] autorelease];
            GDataXMLElement *ring2Element=[elementArray objectAtIndex:0];
            NSArray *ringSubElementArray=[ring2Element elementsForName:@"RingID"];
            if (ringSubElementArray.count>0) {
                [ring2 setRingID:[[ringSubElementArray objectAtIndex:0] stringValue]];
            }
            ringSubElementArray=[ring2Element elementsForName:@"Phases"];
            if (ringSubElementArray.count>0) {
                NSArray *phaseElementArray=[[ringSubElementArray objectAtIndex:0] elementsForName:@"Phase"];
                NSMutableArray *phaseList=[[[NSMutableArray alloc]init]autorelease];
                for (GDataXMLElement *phaseElement in phaseElementArray) {
                    SignalPhase *signalPhase=[[[SignalPhase alloc] init] autorelease];
                    NSArray *phaseSubElementArray=[phaseElement elementsForName:@"PhaseID"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseID:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"PhaseMovement"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseMovement:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"GreenTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setGreenTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"YellowTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setYellowTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"AllRedTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setAllRedTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"Barrier"];
                    if (phaseSubElementArray) {
                        [signalPhase setBarrier:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"Position"];
                    if (phaseSubElementArray) {
                        [signalPhase setPosition:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    [phaseList addObject:signalPhase];
                }
                [ring2 setPhaseList:phaseList];
            }
            [signal setRing2:ring2];
            
        }
        signal.agencyID=agencyID;
        signal.subSystemID=subSystemID;
        signal.timingPatternID=timingPatternID;
        [signalList addObject:signal];
        
        
    }
    return signalList;

}
+ (Signal *) getSignal:(NSString *) signalID agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID timingPatternID:(NSString *)timingPatternID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]",agencyID,subSystemID,timingPatternID,signalID];
    NSArray *signalElementList=[dataDoc nodesForXPath:xPathString error:nil];
    NSLog(@"%@",xPathString);
    //    NSMutableArray * signalList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement * signalElement in signalElementList) {
        Signal *signal=[[[Signal alloc] init] autorelease];
        NSArray *elementArray=[signalElement elementsForName:@"SignalID"];
        if (elementArray.count>0) {
            [signal setSignalID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"SignalName"];
        if (elementArray.count>0) {
            [signal setSignalName:[[elementArray objectAtIndex:0] stringValue]];
        }
//        elementArray=[signalElement elementsForName:@"Coordinate"];
//        if (elementArray.count>0) {
//            GDataXMLElement *coordinateElement=[elementArray objectAtIndex:0];
//            elementArray=[coordinateElement elementsForName:@"Latitude"];
//            double latitude=0;
//            if (elementArray.count>0) {
//                latitude=[[[elementArray objectAtIndex:0] stringValue] floatValue];
//            }
//            elementArray=[coordinateElement elementsForName:@"Longitude"];
//            double longitude=0;
//            if (elementArray.count>0) {
//                longitude=[[[elementArray objectAtIndex:0] stringValue] floatValue];
//            }
//            CLLocationCoordinate2D coordinate={latitude,longitude};
//            [signal setCoordinate:coordinate];
//            
//        }
        elementArray=[signalElement elementsForName:@"CycleLength"];
        if (elementArray.count>0) {
            [signal setCycleLength:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"Offset"];
        if (elementArray.count>0) {
            [signal setOffset:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"OffsetPhase"];
        if (elementArray.count>0) {
            [signal setOffsetPhase:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"OffsetBorE"];
        if (elementArray.count>0) {
            [signal setOffsetBorE:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"Location"];
        if (elementArray.count>0) {
            [signal setLocation:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[signalElement elementsForName:@"Ring1"];
        if (elementArray.count>0) {
            SignalRing *ring1=[[[SignalRing alloc]init] autorelease];
            GDataXMLElement *ring1Element=[elementArray objectAtIndex:0];
            NSArray *ringSubElementArray=[ring1Element elementsForName:@"RingID"];
            if (ringSubElementArray.count>0) {
                [ring1 setRingID:[[ringSubElementArray objectAtIndex:0] stringValue]];
            }
            ringSubElementArray=[ring1Element elementsForName:@"Phases"];
            if (ringSubElementArray.count>0) {
                NSArray *phaseElementArray=[[ringSubElementArray objectAtIndex:0] elementsForName:@"Phase"];
                NSMutableArray *phaseList=[[[NSMutableArray alloc]init]autorelease];
                for (GDataXMLElement *phaseElement in phaseElementArray) {
                    SignalPhase *signalPhase=[[[SignalPhase alloc] init] autorelease];
                    NSArray *phaseSubElementArray=[phaseElement elementsForName:@"PhaseID"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseID:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"PhaseMovement"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseMovement:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"GreenTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setGreenTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"YellowTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setYellowTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"AllRedTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setAllRedTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"Barrier"];
                    if (phaseSubElementArray) {
                        [signalPhase setBarrier:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"Position"];
                    if (phaseSubElementArray) {
                        [signalPhase setPosition:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    [phaseList addObject:signalPhase];
                }
                [ring1 setPhaseList:phaseList];
            }
            [signal setRing1:ring1];
            
        }
        elementArray=[signalElement elementsForName:@"Ring2"];
        if (elementArray.count>0) {
            SignalRing *ring2=[[[SignalRing alloc]init] autorelease];
            GDataXMLElement *ring2Element=[elementArray objectAtIndex:0];
            NSArray *ringSubElementArray=[ring2Element elementsForName:@"RingID"];
            if (ringSubElementArray.count>0) {
                [ring2 setRingID:[[ringSubElementArray objectAtIndex:0] stringValue]];
            }
            ringSubElementArray=[ring2Element elementsForName:@"Phases"];
            if (ringSubElementArray.count>0) {
                NSArray *phaseElementArray=[[ringSubElementArray objectAtIndex:0] elementsForName:@"Phase"];
                NSMutableArray *phaseList=[[[NSMutableArray alloc]init]autorelease];
                for (GDataXMLElement *phaseElement in phaseElementArray) {
                    SignalPhase *signalPhase=[[[SignalPhase alloc] init] autorelease];
                    NSArray *phaseSubElementArray=[phaseElement elementsForName:@"PhaseID"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseID:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"PhaseMovement"];
                    if (phaseSubElementArray) {
                        [signalPhase setPhaseMovement:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"GreenTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setGreenTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"YellowTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setYellowTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"AllRedTime"];
                    if (phaseSubElementArray) {
                        [signalPhase setAllRedTime:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"Barrier"];
                    if (phaseSubElementArray) {
                        [signalPhase setBarrier:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    phaseSubElementArray=[phaseElement elementsForName:@"Position"];
                    if (phaseSubElementArray) {
                        [signalPhase setPosition:[[phaseSubElementArray objectAtIndex:0] stringValue]];
                    }
                    [phaseList addObject:signalPhase];
                }
                [ring2 setPhaseList:phaseList];
            }
            [signal setRing2:ring2];
            
        }
        signal.agencyID=agencyID;
        signal.subSystemID=subSystemID;
        signal.timingPatternID=timingPatternID;
        return signal;
        
        
    }
    return nil;

}
+ (void) updateSignal:(Signal *) signal agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID timingPatternID:(NSString *)timingPatternID
{
    if (!signal) {
        return;
    }
    
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]",agencyID,subSystemID,timingPatternID,signal.signalID];      
    NSArray *elements=[dataDoc nodesForXPath:xPathString error:nil];
    
    if (elements.count<=0) {
        [self createSignal:signal agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPatternID];
    }
    for (GDataXMLElement *element in elements) {
        if ([[element elementsForName:@"SignalName"] count]>0) {
            [[[element elementsForName:@"SignalName"] objectAtIndex:0] setStringValue:signal.signalName];
        }
//        if ([[element elementsForName:@"Coordinate"] count]>0) {
//            GDataXMLElement *coordinateElement=[[element elementsForName:@"Coordinate"] objectAtIndex:0];
//            if ([[coordinateElement elementsForName:@"Latitude"] count]>0) {
//                [[[coordinateElement elementsForName:@"Latitude"] objectAtIndex:0] setStringValue:[NSString stringWithFormat:@"%f",signal.coordinate.latitude]];
//            }
//            if ([[coordinateElement elementsForName:@"Longitude"] count]>0) {
//                [[[coordinateElement elementsForName:@"Longitude"] objectAtIndex:0] setStringValue:[NSString stringWithFormat:@"%f",signal.coordinate.longitude]];
//            }
////            [[[element elementsForName:@"Coordinate"] objectAtIndex:0] setStringValue:signal.signalName];
//        }else {
//            GDataXMLElement *signalCoordinateElement=[GDataXMLElement elementWithName:@"Coordinate"];
//            GDataXMLElement *coordinateLatitudeElement=[GDataXMLElement elementWithName:@"Latitude" stringValue:[NSString stringWithFormat:@"%f",signal.coordinate.latitude]];
//            GDataXMLElement *coordinateLongitudeElement=[GDataXMLElement elementWithName:@"Longitude" stringValue:[NSString stringWithFormat:@"%f",signal.coordinate.longitude]];
//            [signalCoordinateElement addChild:coordinateLatitudeElement];
//            [signalCoordinateElement addChild:coordinateLongitudeElement];
//            [element addChild:signalCoordinateElement];
//            
//        }
        
        
        if ([[element elementsForName:@"CycleLength"] count]>0) {
            [[[element elementsForName:@"CycleLength"] objectAtIndex:0] setStringValue:signal.cycleLength];
        }
        if ([[element elementsForName:@"Offset"] count]>0) {
            [[[element elementsForName:@"Offset"] objectAtIndex:0] setStringValue:signal.offset];
        }
        if ([[element elementsForName:@"OffsetPhase"] count]>0) {
            [[[element elementsForName:@"OffsetPhase"] objectAtIndex:0] setStringValue:signal.offsetPhase];
        }
        if ([[element elementsForName:@"OffsetBorE"] count]>0) {
            [[[element elementsForName:@"OffsetBorE"] objectAtIndex:0] setStringValue:signal.offsetBorE];
        }
        if ([[element elementsForName:@"Location"] count]>0) {
            [[[element elementsForName:@"Location"] objectAtIndex:0] setStringValue:signal.location];
        }
        
    }
    
//    for (int i=0; i<signal.ring1.phaseList.count; i++) {
        [self clearPhases:agencyID subSystemID:subSystemID timingPatternID:timingPatternID signalID:signal.signalID signalRingID:@"1"];
//    }
    for (int i=0; i<signal.ring1.phaseList.count; i++) {
        [self createPhase:[signal.ring1.phaseList objectAtIndex:i] agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPatternID signalID:signal.signalID signalRingID:@"1"];
    }
//    for (int i=0; i<signal.ring2.phaseList.count; i++) {
//        [self removePhase:[[signal.ring2.phaseList objectAtIndex:i] phaseID] agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPatternID signalID:signal.signalID signalRingID:@"2"];
//    }
    [self clearPhases:agencyID subSystemID:subSystemID timingPatternID:timingPatternID signalID:signal.signalID signalRingID:@"2"];
    for (int i=0; i<signal.ring2.phaseList.count; i++) {
        [self createPhase:[signal.ring2.phaseList objectAtIndex:i] agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPatternID signalID:signal.signalID signalRingID:@"2"];
    }
    
    [self saveDataXML];
    

}

+ (NSString *) createSignalID:(NSString *)agencyID subSystemID:(NSString *)subSystemID timingPatternID:(NSString *)timingPatternID
{
    NSArray * elementList=[self getSignalList:agencyID subSystemID:subSystemID timingPatternID:timingPatternID];
    int maxID=-1;
    for (int i=0; i<elementList.count; i++) {
        Signal *element=[elementList objectAtIndex:i];
        if (maxID<[element.signalID intValue]) {
            maxID=[element.signalID intValue];
        }
    }
    maxID=maxID+1;
    return [NSString stringWithFormat:@"%d",maxID];
}

//Operations of links
+ (void)createLink:(Link *)link agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID 
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Links",agencyID,subSystemID];
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
    
    if (parentElements.count<=0) {
        xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]",agencyID,subSystemID];
        parentElements=[dataDoc nodesForXPath:xPathString error:nil];
        GDataXMLElement *linksElement=[GDataXMLElement elementWithName:@"Links"];
        if (parentElements.count<=0) {
            return;
        }
        [[parentElements objectAtIndex:0] addChild:linksElement];
        xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Links",agencyID,subSystemID];
        parentElements=[dataDoc nodesForXPath:xPathString error:nil];
        if (parentElements.count<=0) {
            return;
        }
    }
    GDataXMLElement *linkElement=[GDataXMLElement elementWithName:@"Link"];
    GDataXMLElement *linkIDElement=[GDataXMLElement elementWithName:@"LinkID" stringValue:link.linkID];
    GDataXMLElement *linkNameElement=[GDataXMLElement elementWithName:@"LinkName" stringValue:link.linkName];
    GDataXMLElement * fromSignalIDElement=[GDataXMLElement elementWithName:@"FromIntersectionID" stringValue:link.fromIntersectionID];
    GDataXMLElement * toSignalIDElement=[GDataXMLElement elementWithName:@"ToIntersectionID" stringValue:link.toIntersectionID];
    GDataXMLElement * linkDirectionElement=[GDataXMLElement elementWithName:@"LinkDirection" stringValue:link.linkDirection];
    GDataXMLElement * linkLengthElement=[GDataXMLElement elementWithName:@"Length" stringValue:link.length];
    GDataXMLElement * linkSpeedLimitElement=[GDataXMLElement elementWithName:@"SpeedLimit" stringValue:link.speedLimit];
    
    
    [linkElement addChild:linkIDElement];
    [linkElement addChild:linkNameElement];
    [linkElement addChild:fromSignalIDElement];
    [linkElement addChild:toSignalIDElement];
    [linkElement addChild:linkDirectionElement];
    [linkElement addChild:linkLengthElement];
    [linkElement addChild:linkSpeedLimitElement];
    [[parentElements objectAtIndex:0] addChild:linkElement];
    
     [self saveDataXML];
    
    
}
+ (void) removeLink:(NSString *)linkID agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Links",agencyID,subSystemID];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
    if (parentElements.count<=0) {
        return;
    }
    
    xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Links/Link[LinkID=\"%@\"]",agencyID,subSystemID,linkID];
    
    NSArray *childElements=[dataDoc nodesForXPath:xPathString error:nil];
    if (childElements.count<=0) {
        return;
    }
    for (int i=0; i<childElements.count; i++) {
        [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
    }
    [self saveDataXML];
}
+ (NSArray *) getLinkList:(NSString *)agencyID subSystemID:(NSString *)subSystemID 
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Links/Link",agencyID,subSystemID];
    
    NSArray *linkElementList=[dataDoc nodesForXPath:xPathString error:nil];
    
    NSMutableArray * linkList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement * linkElement in linkElementList) {
        Link *link=[[[Link alloc] init] autorelease];
        NSArray *elementArray=[linkElement elementsForName:@"LinkID"];
        if (elementArray.count>0) {
            [link setLinkID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"LinkName"];
        if (elementArray.count>0) {
            [link setLinkName:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"FromIntersectionID"];
        if (elementArray.count>0) {
            [link setFromIntersectionID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"ToIntersectionID"];
        if (elementArray.count>0) {
            [link setToIntersectionID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"LinkDirection"];
        if (elementArray.count>0) {
            [link setLinkDirection:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"Length"];
        if (elementArray.count>0) {
            [link setLength:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"SpeedLimit"];
        if (elementArray.count>0) {
            [link setSpeedLimit:[[elementArray objectAtIndex:0] stringValue]];
        }
        
       
        [linkList addObject:link];
        
        
    }
    return linkList;
    
}
+ (NSArray *) getLinkListFromIntersection:(NSString *)agencyID subSystemID:(NSString *)subSystemID intersectionID:(NSString *)intersectionID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Links/Link[FromIntersectionID=\"%@\"]",agencyID,subSystemID,intersectionID];
    
    NSArray *linkElementList=[dataDoc nodesForXPath:xPathString error:nil];
    
    NSMutableArray * linkList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement * linkElement in linkElementList) {
        Link *link=[[[Link alloc] init] autorelease];
        NSArray *elementArray=[linkElement elementsForName:@"LinkID"];
        if (elementArray.count>0) {
            [link setLinkID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"LinkName"];
        if (elementArray.count>0) {
            [link setLinkName:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"FromIntersectionID"];
        if (elementArray.count>0) {
            [link setFromIntersectionID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"ToIntersectionID"];
        if (elementArray.count>0) {
            [link setToIntersectionID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"LinkDirection"];
        if (elementArray.count>0) {
            [link setLinkDirection:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"Length"];
        if (elementArray.count>0) {
            [link setLength:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"SpeedLimit"];
        if (elementArray.count>0) {
            [link setSpeedLimit:[[elementArray objectAtIndex:0] stringValue]];
        }
        
        
        [linkList addObject:link];
        
        
    }
    return linkList;
    
}

+ (NSArray *) getLinkListToIntersection:(NSString *)agencyID subSystemID:(NSString *)subSystemID intersectionID:(NSString *)intersectionID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Links/Link[ToIntersectionID=\"%@\"]",agencyID,subSystemID,intersectionID];
    
    NSArray *linkElementList=[dataDoc nodesForXPath:xPathString error:nil];
    
    NSMutableArray * linkList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement * linkElement in linkElementList) {
        Link *link=[[[Link alloc] init] autorelease];
        NSArray *elementArray=[linkElement elementsForName:@"LinkID"];
        if (elementArray.count>0) {
            [link setLinkID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"LinkName"];
        if (elementArray.count>0) {
            [link setLinkName:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"FromIntersectionID"];
        if (elementArray.count>0) {
            [link setFromIntersectionID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"ToIntersectionID"];
        if (elementArray.count>0) {
            [link setToIntersectionID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"LinkDirection"];
        if (elementArray.count>0) {
            [link setLinkDirection:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"Length"];
        if (elementArray.count>0) {
            [link setLength:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"SpeedLimit"];
        if (elementArray.count>0) {
            [link setSpeedLimit:[[elementArray objectAtIndex:0] stringValue]];
        }
        
        
        [linkList addObject:link];
        
        
    }
    return linkList;
    
}
+ (Link *) getLink:(NSString *)linkID agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID 
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Links/Link[LinkID=\"%@\"]",agencyID,subSystemID,linkID];
    NSArray *linkElementList=[dataDoc nodesForXPath:xPathString error:nil];
//    NSLog(@"%@",xPathString);
    //    NSMutableArray * signalList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement * linkElement in linkElementList) {
        Link *link=[[[Link alloc] init] autorelease];
        NSArray *elementArray=[linkElement elementsForName:@"LinkID"];
        if (elementArray.count>0) {
            [link setLinkID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"LinkName"];
        if (elementArray.count>0) {
            [link setLinkName:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"FromIntersectionID"];
        if (elementArray.count>0) {
            [link setFromIntersectionID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"ToIntersectionID"];
        if (elementArray.count>0) {
            [link setToIntersectionID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"LinkDirection"];
        if (elementArray.count>0) {
            [link setLinkDirection:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"Length"];
        if (elementArray.count>0) {
            [link setLength:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[linkElement elementsForName:@"SpeedLimit"];
        if (elementArray.count>0) {
            [link setSpeedLimit:[[elementArray objectAtIndex:0] stringValue]];
        }
        
        
        return link;
        
        
    }

    
    return nil;
    
}
+ (void) updateLink:(Link *)link agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    if (!link) {
        return;
    }
    
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Links/Link[LinkID=\"%@\"]",agencyID,subSystemID,link.linkID];
    NSArray *elements=[dataDoc nodesForXPath:xPathString error:nil];
    
    if (elements.count<=0) {
        [self createLink:link agencyID:agencyID subSystemID:subSystemID];
    }
    for (GDataXMLElement *element in elements) {
        if ([[element elementsForName:@"LinkName"] count]>0) {
            [[[element elementsForName:@"LinkName"] objectAtIndex:0] setStringValue:link.linkName];
            
        }
        if ([[element elementsForName:@"FromIntersectionID"] count]>0) {
            [[[element elementsForName:@"FromIntersectionID"] objectAtIndex:0] setStringValue:link.fromIntersectionID];
        }
        if ([[element elementsForName:@"ToIntersectionID"] count]>0) {
            [[[element elementsForName:@"ToIntersectionID"] objectAtIndex:0] setStringValue:link.toIntersectionID];
        }
        if ([[element elementsForName:@"LinkDirection"] count]>0) {
            [[[element elementsForName:@"LinkDirection"] objectAtIndex:0] setStringValue:link.linkDirection];
        }
        if ([[element elementsForName:@"Length"] count]>0) {
            [[[element elementsForName:@"Length"] objectAtIndex:0] setStringValue:link.length];
        }
        if ([[element elementsForName:@"SpeedLimit"] count]>0) {
            [[[element elementsForName:@"SpeedLimit"] objectAtIndex:0] setStringValue:link.speedLimit];
        }
        
        
    }
    
    
    [self saveDataXML];
    
    
}

+ (NSString *) createLinkID:(NSString *)agencyID subSystemID:(NSString *)subSystemID 
{
    NSArray * elementList=[self getLinkList:agencyID subSystemID:subSystemID ];
    int maxID=-1;
    for (int i=0; i<elementList.count; i++) {
        Link *element=[elementList objectAtIndex:i];
        if (maxID<[element.linkID intValue]) {
            maxID=[element.linkID intValue];
        }
    }
    maxID=maxID+1;
    return [NSString stringWithFormat:@"%d",maxID];
}

//Operations of intersections
+ (void)createIntersection:(Intersection *)intersection agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Intersections",agencyID,subSystemID];
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
    
    if (parentElements.count<=0) {
        xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]",agencyID,subSystemID];
        parentElements=[dataDoc nodesForXPath:xPathString error:nil];
        GDataXMLElement *intersectionsElement=[GDataXMLElement elementWithName:@"Intersections"];
        if (parentElements.count<=0) {
            return;
        }
        [[parentElements objectAtIndex:0] addChild:intersectionsElement];
        xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Intersections",agencyID,subSystemID];
        parentElements=[dataDoc nodesForXPath:xPathString error:nil];
        if (parentElements.count<=0) {
            return;
        }
    }
    GDataXMLElement *intersectionElement=[GDataXMLElement elementWithName:@"Intersection"];
    GDataXMLElement *intersectionIDElement=[GDataXMLElement elementWithName:@"IntersectionID" stringValue:intersection.intersectionID];
    GDataXMLElement *intersectionNameElement=[GDataXMLElement elementWithName:@"IntersectionName" stringValue:intersection.intersectionName];
    
    [intersectionElement addChild:intersectionIDElement];
    [intersectionElement addChild:intersectionNameElement];
    {
        GDataXMLElement *coordinateElement=[GDataXMLElement elementWithName:@"IntersectionCoordinate"];
        GDataXMLElement *coordinateLatitudeElement=[GDataXMLElement elementWithName:@"Latitude" stringValue:[NSString stringWithFormat:@"%f",intersection.intersectionCoordinate.latitude]];
        GDataXMLElement *coordinateLongitudeElement=[GDataXMLElement elementWithName:@"Longitude" stringValue:[NSString stringWithFormat:@"%f",intersection.intersectionCoordinate.longitude]];
        [coordinateElement addChild:coordinateLatitudeElement];
        [coordinateElement addChild:coordinateLongitudeElement];
        [intersectionElement addChild:coordinateElement];
    }
    [[parentElements objectAtIndex:0] addChild:intersectionElement];
    
    NSArray *timingPatternList=[self getTimingPatternList:agencyID subSystemID:subSystemID];
    for (TimingPattern *timingPattern in timingPatternList) {
//        TimingPattern *timingPattern=[timePatternList objectAtIndex:i];
        Signal *signal=[[[NEMASignal alloc] initWithCycleLength:timingPattern.cycleLength] autorelease];
        [signal setSignalID:intersection.intersectionID];
        //    [signal setCycleLength:self.timingPattern.cycleLength];
        [signal setOffset:@"0"];
        [signal setSignalName:intersection.intersectionName];
        [XMLParser createSignal:signal agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPattern.timingPatternID];
    }
    
     [self saveDataXML];
    
}
+ (void) removeIntersection:(NSString *)intersectionID agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Intersections",agencyID,subSystemID];
    
    NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
    if (parentElements.count<=0) {
        return;
    }
    
    xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Intersections/Intersection[IntersectionID=\"%@\"]",agencyID,subSystemID,intersectionID];
    
    NSArray *childElements=[dataDoc nodesForXPath:xPathString error:nil];
    if (childElements.count<=0) {
        return;
    }
    for (int i=0; i<childElements.count; i++) {
        [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
    }
    NSArray *timingPatternList=[self getTimingPatternList:agencyID subSystemID:subSystemID];
    for (TimingPattern *timingPattern in timingPatternList) {
        [self removeSignal:intersectionID agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPattern.timingPatternID];
    }
    
    [self saveDataXML];
}
+ (NSArray *) getIntersectionList:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Intersections/Intersection",agencyID,subSystemID];
    
    NSArray *intersectionElementList=[dataDoc nodesForXPath:xPathString error:nil];
    
    NSMutableArray * intersectionList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement * intersectionElement in intersectionElementList) {
        Intersection *intersection=[[[Intersection alloc] init] autorelease];
        NSArray *elementArray=[intersectionElement elementsForName:@"IntersectionID"];
        if (elementArray.count>0) {
            [intersection setIntersectionID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[intersectionElement elementsForName:@"IntersectionName"];
        if (elementArray.count>0) {
            [intersection setIntersectionName:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[intersectionElement elementsForName:@"IntersectionCoordinate"];
        if (elementArray.count>0) {
            GDataXMLElement *coordinateElement=[elementArray objectAtIndex:0];
            elementArray=[coordinateElement elementsForName:@"Latitude"];
            double latitude=0;
            if (elementArray.count>0) {
                latitude=[[[elementArray objectAtIndex:0] stringValue] floatValue];
            }
            elementArray=[coordinateElement elementsForName:@"Longitude"];
            double longitude=0;
            if (elementArray.count>0) {
                longitude=[[[elementArray objectAtIndex:0] stringValue] floatValue];
            }
            CLLocationCoordinate2D coordinate={latitude,longitude};
            [intersection setIntersectionCoordinate:coordinate];
            
        }
        
        
        
        [intersectionList addObject:intersection];
        
        
    }
    return intersectionList;
    
}
+ (Intersection *) getIntersection:(NSString *)intersectionID agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Intersections/Intersection[IntersectionID=\"%@\"]",agencyID,subSystemID,intersectionID];
    NSArray *intersectionElementList=[dataDoc nodesForXPath:xPathString error:nil];
    
//    NSMutableArray * intersectionList=[[[NSMutableArray alloc] init] autorelease];
    for (GDataXMLElement * intersectionElement in intersectionElementList) {
        Intersection *intersection=[[[Intersection alloc] init] autorelease];
        NSArray *elementArray=[intersectionElement elementsForName:@"IntersectionID"];
        if (elementArray.count>0) {
            [intersection setIntersectionID:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[intersectionElement elementsForName:@"IntersectionName"];
        if (elementArray.count>0) {
            [intersection setIntersectionName:[[elementArray objectAtIndex:0] stringValue]];
        }
        elementArray=[intersectionElement elementsForName:@"IntersectionCoordinate"];
        if (elementArray.count>0) {
            GDataXMLElement *coordinateElement=[elementArray objectAtIndex:0];
            elementArray=[coordinateElement elementsForName:@"Latitude"];
            double latitude=0;
            if (elementArray.count>0) {
                latitude=[[[elementArray objectAtIndex:0] stringValue] floatValue];
            }
            elementArray=[coordinateElement elementsForName:@"Longitude"];
            double longitude=0;
            if (elementArray.count>0) {
                longitude=[[[elementArray objectAtIndex:0] stringValue] floatValue];
            }
            CLLocationCoordinate2D coordinate={latitude,longitude};
            [intersection setIntersectionCoordinate:coordinate];
            
        }
        
        
        
        return intersection;
        
        
    }
    
    
    return nil;
    
}
+ (void) updateIntersection:(Intersection *)intersection agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    if (!link) {
        return;
    }
    
    NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/Intersections/Intersection[IntersectionID=\"%@\"]",agencyID,subSystemID,intersection.intersectionID];
    NSArray *elements=[dataDoc nodesForXPath:xPathString error:nil];
    
    if (elements.count<=0) {
        [self createIntersection:intersection agencyID:agencyID subSystemID:subSystemID];
    }
    for (GDataXMLElement *element in elements) {
        if ([[element elementsForName:@"IntersectionName"] count]>0) {
            [[[element elementsForName:@"IntersectionName"] objectAtIndex:0] setStringValue:intersection.intersectionName];
            
        }
        if ([[element elementsForName:@"IntersectionCoordinate"] count]>0) {
            GDataXMLElement *coordinateElement=[[element elementsForName:@"IntersectionCoordinate"] objectAtIndex:0];
            if ([[coordinateElement elementsForName:@"Latitude"] count]>0) {
                [[[coordinateElement elementsForName:@"Latitude"] objectAtIndex:0] setStringValue:[NSString stringWithFormat:@"%f",intersection.intersectionCoordinate.latitude]];
            }
            if ([[coordinateElement elementsForName:@"Longitude"] count]>0) {
                [[[coordinateElement elementsForName:@"Longitude"] objectAtIndex:0] setStringValue:[NSString stringWithFormat:@"%f",intersection.intersectionCoordinate.longitude]];
            }
            //            [[[element elementsForName:@"Coordinate"] objectAtIndex:0] setStringValue:signal.signalName];
        }else {
            GDataXMLElement *coordinateElement=[GDataXMLElement elementWithName:@"IntersectionCoordinate"];
            GDataXMLElement *coordinateLatitudeElement=[GDataXMLElement elementWithName:@"Latitude" stringValue:[NSString stringWithFormat:@"%f",intersection.intersectionCoordinate.latitude]];
            GDataXMLElement *coordinateLongitudeElement=[GDataXMLElement elementWithName:@"Longitude" stringValue:[NSString stringWithFormat:@"%f",intersection.intersectionCoordinate.longitude]];
            [coordinateElement addChild:coordinateLatitudeElement];
            [coordinateElement addChild:coordinateLongitudeElement];
            [element addChild:coordinateElement];
//            [element addChild:signalCoordinateElement];
            
        }
        
    }
    
    
    [self saveDataXML];
    
    
}

+ (NSString *) createIntersectionID:(NSString *)agencyID subSystemID:(NSString *)subSystemID
{
    NSArray * elementList=[self getIntersectionList:agencyID subSystemID:subSystemID ];
    int maxID=-1;
    for (int i=0; i<elementList.count; i++) {
        Intersection *element=[elementList objectAtIndex:i];
        if (maxID<[element.intersectionID intValue]) {
            maxID=[element.intersectionID intValue];
        }
    }
    maxID=maxID+1;
    return [NSString stringWithFormat:@"%d",maxID];
}


//Operations of Phases
+ (void)createPhase:(SignalPhase *) phase agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID timingPatternID:(NSString *)timingPatternID signalID:(NSString *)signalID signalRingID:(NSString *)signalRingID
{
    
    if ([signalRingID isEqualToString:@"1"]) {
         NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]/Ring1/Phases",agencyID,subSystemID,timingPatternID,signalID];
          
        NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
        if (parentElements.count<=0) {
            return;
        }
        
        GDataXMLElement *phaseElement=[GDataXMLElement elementWithName:@"Phase"];
        GDataXMLElement *phaseIDElement=[GDataXMLElement elementWithName:@"PhaseID" stringValue:phase.phaseID];
        GDataXMLElement *phaseMovementElement=[GDataXMLElement elementWithName:@"PhaseMovement" stringValue:phase.phaseMovement];
        GDataXMLElement *phaseGreenTime=[GDataXMLElement elementWithName:@"GreenTime" stringValue:phase.greenTime];
        GDataXMLElement *phaseYellowTime=[GDataXMLElement elementWithName:@"YellowTime" stringValue:phase.yellowTime];
        GDataXMLElement *phaseAllRedTime=[GDataXMLElement elementWithName:@"AllRedTime" stringValue:phase.allRedTime];
        GDataXMLElement *barrierElement=[GDataXMLElement elementWithName:@"Barrier" stringValue:phase.barrier];
        GDataXMLElement *positionElement=[GDataXMLElement elementWithName:@"Position" stringValue:phase.position];
        [phaseElement addChild:phaseIDElement];
        [phaseElement addChild:phaseMovementElement];
        [phaseElement addChild:barrierElement];
        [phaseElement addChild:positionElement];
        [phaseElement addChild:phaseGreenTime];
        [phaseElement addChild:phaseYellowTime];
        [phaseElement addChild:phaseAllRedTime];
        [[parentElements objectAtIndex:0] addChild:phaseElement];
        [self saveDataXML];
    }else {
        NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]/Ring2/Phases",agencyID,subSystemID,timingPatternID,signalID];
        
        NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
        if (parentElements.count<=0) {
            return;
        }
        
        GDataXMLElement *phaseElement=[GDataXMLElement elementWithName:@"Phase"];
        GDataXMLElement *phaseIDElement=[GDataXMLElement elementWithName:@"PhaseID" stringValue:phase.phaseID];
        GDataXMLElement *phaseMovementElement=[GDataXMLElement elementWithName:@"PhaseMovement" stringValue:phase.phaseMovement];
        GDataXMLElement *phaseGreenTime=[GDataXMLElement elementWithName:@"GreenTime" stringValue:phase.greenTime];
        GDataXMLElement *phaseYellowTime=[GDataXMLElement elementWithName:@"YellowTime" stringValue:phase.yellowTime];
        GDataXMLElement *phaseAllRedTime=[GDataXMLElement elementWithName:@"AllRedTime" stringValue:phase.allRedTime];
        GDataXMLElement *barrierElement=[GDataXMLElement elementWithName:@"Barrier" stringValue:phase.barrier];
        GDataXMLElement *positionElement=[GDataXMLElement elementWithName:@"Position" stringValue:phase.position];
        [phaseElement addChild:phaseIDElement];
        [phaseElement addChild:phaseIDElement];
        [phaseElement addChild:phaseMovementElement];
        [phaseElement addChild:barrierElement];
        [phaseElement addChild:positionElement];
        [phaseElement addChild:phaseGreenTime];
        [phaseElement addChild:phaseYellowTime];
        [phaseElement addChild:phaseAllRedTime];
        [[parentElements objectAtIndex:0] addChild:phaseElement];
        [self saveDataXML];
        
    }

}
+ (void) removePhase:(NSString *) phaseID agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID timingPatternID:(NSString *)timingPatternID signalID:(NSString *)signalID signalRingID:(NSString *)signalRingID
{
    if ([signalRingID isEqualToString:@"1"]) {
        NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]/Ring1/Phases",agencyID,subSystemID,timingPatternID,signalID];
        NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
        if (parentElements.count<=0) {
            return;
        }
        xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]/Ring1/Phases/Phase[PhaseID=\"%@\"]",agencyID,subSystemID,timingPatternID,signalID,phaseID];  
        
        NSArray *childElements=[dataDoc nodesForXPath:xPathString error:nil];
        if (childElements.count<=0) {
            return;
        }
        for (int i=0; i<childElements.count; i++) {
            [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
        }
        [self saveDataXML];
    }
    else {
        NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]/Ring2/Phases",agencyID,subSystemID,timingPatternID,signalID];
        
        NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
        if (parentElements.count<=0) {
            return;
        }
        xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]/Ring2/Phases/Phase[PhaseID=\"%@\"]",agencyID,subSystemID,timingPatternID,signalID,phaseID];  
        
        NSArray *childElements=[dataDoc nodesForXPath:xPathString error:nil];
        if (childElements.count<=0) {
            return;
        }
        for (int i=0; i<childElements.count; i++) {
            [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
        }
        [self saveDataXML];
    }
   
    
    
}
+ (void) updatePhase:(SignalPhase *) phase agencyID:(NSString *)agencyID subSystemID:(NSString *)subSystemID timingPatternID:(NSString *)timingPatternID signalID:(NSString *)signalID signalRingID:(NSString *)signalRingID
{
    
    if ([signalRingID isEqualToString:@"1"]) {
       
        NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]/Ring1/Phases/Phase[PhaseID=\"%@\"]",agencyID,subSystemID,timingPatternID,signalID,phase.phaseID]; 
        
        NSArray *elements=[dataDoc nodesForXPath:xPathString error:nil];
        if (elements.count<=0) {
            [self createPhase:phase agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPatternID signalID:signalID signalRingID:signalRingID];
        }
        for (GDataXMLElement *element in elements) {
            if([[element elementsForName:@"PhaseMovement"] count]>0)
            {
                [[[element elementsForName:@"PhaseMovement"] objectAtIndex:0] setStringValue:phase.phaseMovement];
            }
            if([[element elementsForName:@"GreenTime"] count]>0)
            {
                [[[element elementsForName:@"GreenTime"] objectAtIndex:0] setStringValue:phase.greenTime];
            }
            if([[element elementsForName:@"YellowTime"] count]>0)
            {
                [[[element elementsForName:@"YellowTime"] objectAtIndex:0] setStringValue:phase.yellowTime];
            }
            if([[element elementsForName:@"AllRedTime"] count]>0)
            {
                [[[element elementsForName:@"AllRedTime"] objectAtIndex:0] setStringValue:phase.allRedTime];
            }
            if([[element elementsForName:@"Barrier"] count]>0)
            {
                [[[element elementsForName:@"Barrier"] objectAtIndex:0] setStringValue:phase.barrier];
            }
            if([[element elementsForName:@"Position"] count]>0)
            {
                [[[element elementsForName:@"Position"] objectAtIndex:0] setStringValue:phase.position];
            }
        }
        
        [self saveDataXML];
    }else {
        NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]/Ring2/Phases/Phase[PhaseID=\"%@\"]",agencyID,subSystemID,timingPatternID,signalID,phase.phaseID]; 
        
        NSArray *elements=[dataDoc nodesForXPath:xPathString error:nil];
        if (elements.count<=0) {
            [self createPhase:phase agencyID:agencyID subSystemID:subSystemID timingPatternID:timingPatternID signalID:signalID signalRingID:signalRingID];
        }
        for (GDataXMLElement *element in elements) {
            if([[element elementsForName:@"PhaseMovement"] count]>0)
            {
                [[[element elementsForName:@"PhaseMovement"] objectAtIndex:0] setStringValue:phase.phaseMovement];
            }
            if([[element elementsForName:@"GreenTime"] count]>0)
            {
                [[[element elementsForName:@"GreenTime"] objectAtIndex:0] setStringValue:phase.greenTime];
            }
            if([[element elementsForName:@"YellowTime"] count]>0)
            {
                [[[element elementsForName:@"YellowTime"] objectAtIndex:0] setStringValue:phase.yellowTime];
            }
            if([[element elementsForName:@"AllRedTime"] count]>0)
            {
                [[[element elementsForName:@"AllRedTime"] objectAtIndex:0] setStringValue:phase.allRedTime];
            }
            if([[element elementsForName:@"Barrier"] count]>0)
            {
                [[[element elementsForName:@"Barrier"] objectAtIndex:0] setStringValue:phase.barrier];
            }
            if([[element elementsForName:@"Position"] count]>0)
            {
                [[[element elementsForName:@"Position"] objectAtIndex:0] setStringValue:phase.position];
            }
        }
        
        [self saveDataXML];
    }

}
+ (void) clearPhases:(NSString *)agencyID subSystemID:(NSString *)subSystemID timingPatternID:(NSString *)timingPatternID signalID:(NSString *)signalID signalRingID:(NSString *)signalRingID
{
    if ([signalRingID isEqualToString:@"1"]) {
        NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]/Ring1/Phases",agencyID,subSystemID,timingPatternID,signalID];
        NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
        if (parentElements.count<=0) {
            return;
        }
        xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]/Ring1/Phases/Phase",agencyID,subSystemID,timingPatternID,signalID];  
        
        NSArray *childElements=[dataDoc nodesForXPath:xPathString error:nil];
        if (childElements.count<=0) {
            return;
        }
        for (int i=0; i<childElements.count; i++) {
            [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
        }
        [self saveDataXML];
    }
    else {
        NSString *xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]/Ring2/Phases",agencyID,subSystemID,timingPatternID,signalID];
        
        NSArray *parentElements=[dataDoc nodesForXPath:xPathString error:nil];
        if (parentElements.count<=0) {
            return;
        }
        xPathString=[dataXMLRoot stringByAppendingFormat:@"/Agencies/Agency[AgencyID=\"%@\"]/SubSystems/SubSystem[SubSystemID=\"%@\"]/TimingPatterns/TimingPattern[TimingPatternID=\"%@\"]/Signals/Signal[SignalID=\"%@\"]/Ring2/Phases/Phase",agencyID,subSystemID,timingPatternID,signalID];  
        
        NSArray *childElements=[dataDoc nodesForXPath:xPathString error:nil];
        if (childElements.count<=0) {
            return;
        }
        for (int i=0; i<childElements.count; i++) {
            [[parentElements objectAtIndex:0] removeChild:[childElements objectAtIndex:i]];
        }
        [self saveDataXML];
    }
    
    
    
}


@end
